Skip to contentSkip to conversation

tls: Pass optional data parameter to sign/verify callbacks

PATCH In review Andrew Zaborowski 2 participants 4 messages Jan 11, 2019 – Jan 17, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tls: Pass optional data parameter to sign/verify callbacks

Add parameters to optinally provide a buffer with the data to be signed/verified to the signature algorithm .sign and .verify functions, in addition to the current get_hash callback parameter. Currently .sign and .verify would receive the get_hash callback and call it when necessary to obtain the hash of the data being signed.

In the case of the handshake messages signature the hash data is part of the handshake state so there was no need for additional parameters. When we're hashing the ServerKeyExchange parameters though there's no need to keep a buffer of the data around for longer than we're building or processing the message so there's no point copying it to the handshake state structs. So instead we pass that buffer to .sign/.verify which then passes it to the get_hash callbacks.

This removes some duplication and in case of ECDHE gets rid of two instances of rebuilding the ServerECDHParams struct unnecessarily.
---
This is to illustrate the possible cleanup in DHE to avoid putting a pointer to a stack structure in the handshake state.

---
 ell/tls-private.h |   3 +
 ell/tls-suites.c  | 198 ++++++++++++++++++++--------------------------
 ell/tls.c         |   6 +-
 3 files changed, 91 insertions(+), 116 deletions(-)
ell/tls-private.h +3 -0
index c82a4d5..134203f 100644
@@ -59,6 +59,7 @@ struct tls_hash_algorithm {
 extern const struct tls_hash_algorithm tls_handshake_hash_data[];
 
 typedef bool (*tls_get_hash_t)(struct l_tls *tls, uint8_t tls_id,
+				const uint8_t *data, size_t data_len,
 				uint8_t *out, size_t *len,
 				enum l_checksum_type *type);
 
@@ -80,8 +81,10 @@ struct tls_key_exchange_algorithm {
 						const uint8_t *buf, size_t len);
 
 	ssize_t (*sign)(struct l_tls *tls, uint8_t *out, size_t len,
+			const uint8_t *data, size_t data_len,
 			tls_get_hash_t get_hash);
 	bool (*verify)(struct l_tls *tls, const uint8_t *in, size_t len,
+			const uint8_t *data, size_t data_len,
 			tls_get_hash_t get_hash);
 
 	void (*free_params)(struct l_tls *tls);
ell/tls-suites.c +84 -114

341 lines not on this page · raw message

ell/tls.c +4 -2
index f9e1e95..6e8c968 100644
@@ -1143,6 +1143,7 @@ static void tls_get_handshake_hash(struct l_tls *tls,
 }
 
 static bool tls_get_handshake_hash_by_id(struct l_tls *tls, uint8_t hash_id,
+					const uint8_t *data, size_t data_len,
 					uint8_t *out, size_t *len,
 					enum l_checksum_type *type)
 {
@@ -1176,7 +1177,7 @@ static bool tls_send_certificate_verify(struct l_tls *tls)
 	sign_len = tls->pending.cipher_suite->key_xchg->sign(tls,
 					buf + TLS_HANDSHAKE_HEADER_SIZE,
 					2048 - TLS_HANDSHAKE_HEADER_SIZE,
-					tls_get_handshake_hash_by_id);
+					NULL, 0, tls_get_handshake_hash_by_id);
 
 	if (sign_len < 0)
 		return false;
@@ -1997,6 +1998,7 @@ static void tls_handle_server_hello_done(struct l_tls *tls,
 }
 
 static bool tls_get_prev_digest_by_id(struct l_tls *tls, uint8_t hash_id,
+					const uint8_t *data, size_t data_len,
 					uint8_t *out, size_t *out_len,
 					enum l_checksum_type *type)
 {
@@ -2026,7 +2028,7 @@ static void tls_handle_certificate_verify(struct l_tls *tls,
 {
 	int i;
 
-	if (!tls->pending.cipher_suite->key_xchg->verify(tls, buf, len,
+	if (!tls->pending.cipher_suite->key_xchg->verify(tls, buf, len, NULL, 0,
 						tls_get_prev_digest_by_id))
 		return;
 
Signature · 1 line
2.19.1
#1 lore raw

PATCH 2/2 tls: Separate signature algorithm and key exchange algorithm

Separate definitions of the key exchange algorithms from the signature algorithm. DHE and ECDHE can be used with different signature algorithms hopefully without any changes.

---
 ell/tls-private.h |  24 +--
 ell/tls-suites.c  | 369 +++++++++++++++++++++++++---------------------
 ell/tls.c         |  44 +++---
 3 files changed, 238 insertions(+), 199 deletions(-)
ell/tls-private.h +13 -11
index 134203f..10f25c5 100644
@@ -63,15 +63,23 @@ 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);
 
-struct tls_key_exchange_algorithm {
+struct tls_signature_algorithm {
 	uint8_t id;
 
-	bool certificate_check;
+	bool (*validate_cert_key_type)(struct l_cert *cert);
+
+	ssize_t (*sign)(struct l_tls *tls, uint8_t *out, size_t len,
+			const uint8_t *data, size_t data_len,
+			tls_get_hash_t get_hash);
+	bool (*verify)(struct l_tls *tls, const uint8_t *in, size_t len,
+			const uint8_t *data, size_t data_len,
+			tls_get_hash_t get_hash);
+};
+
+struct tls_key_exchange_algorithm {
 	bool need_ecc;
 	bool need_ffdh;
 
-	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);
@@ -80,13 +88,6 @@ struct tls_key_exchange_algorithm {
 	void (*handle_client_key_exchange)(struct l_tls *tls,
 						const uint8_t *buf, size_t len);
 
-	ssize_t (*sign)(struct l_tls *tls, uint8_t *out, size_t len,
-			const uint8_t *data, size_t data_len,
-			tls_get_hash_t get_hash);
-	bool (*verify)(struct l_tls *tls, const uint8_t *in, size_t len,
-			const uint8_t *data, size_t data_len,
-			tls_get_hash_t get_hash);
-
 	void (*free_params)(struct l_tls *tls);
 };
 
@@ -102,6 +103,7 @@ struct tls_cipher_suite {
 	int verify_data_length;
 
 	struct tls_bulk_encryption_algorithm *encryption;
+	struct tls_signature_algorithm *signature;
 	struct tls_key_exchange_algorithm *key_xchg;
 	struct tls_mac_algorithm *mac;
 	enum l_checksum_type prf_hmac;
ell/tls-suites.c +200 -169

589 lines not on this page · raw message

ell/tls.c +25 -19

133 lines not on this page · raw message

Signature · 1 line
2.19.1
#2 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 19 lines · Andrew Zaborowski

Add parameters to optinally provide a buffer with the data to be signed/verified to the signature algorithm .sign and .verify functions, in addition to the current get_hash callback parameter. Currently .sign and .verify would receive the get_hash callback and call it when necessary to obtain the hash of the data being signed.

In the case of the handshake messages signature the hash data is part of the handshake state so there was no need for additional parameters. When we're hashing the ServerKeyExchange parameters though there's no need to keep a buffer of the data around for longer than we're building or processing the message so there's no point copying it to the handshake state structs. So instead we pass that buffer to .sign/.verify which then passes it to the get_hash callbacks.

This removes some duplication and in case of ECDHE gets rid of two instances of rebuilding the ServerECDHParams struct unnecessarily.
---
This is to illustrate the possible cleanup in DHE to avoid putting a pointer to a stack structure in the handshake state.

Quoted 5 lines
---
  ell/tls-private.h |   3 +
  ell/tls-suites.c  | 198 ++++++++++++++++++++--------------------------
  ell/tls.c         |   6 +-
  3 files changed, 91 insertions(+), 116 deletions(-)

So I like this approach way more than the current situation.

ell/tls-private.h struct tls_hash_algorithm { · line 63

Quoted 11 lines
ell/tls-private.h +1 -0
index c82a4d5..134203f 100644
@@ -59,6 +59,7 @@ struct tls_hash_algorithm {
  extern const struct tls_hash_algorithm tls_handshake_hash_data[];
 
  typedef bool (*tls_get_hash_t)(struct l_tls *tls, uint8_t tls_id,
+				const uint8_t *data, size_t data_len,
  				uint8_t *out, size_t *len,

Can we call this out_len to make this clearer?

ell/tls-private.h struct tls_hash_algorithm { · line 64

Quoted 1 line
ell/tls-private.h +0 -0
  				enum l_checksum_type *type);

So one question I have is whether this type argument is needed. It seems like you only ever use it in 1 place, and pass in NULL everywhere else.

You could simply add a convenience method to lookup l_checksum from a tls_id, no?

ell/tls-private.h struct tls_key_exchange_algorithm { · line 83

Quoted 5 lines
ell/tls-private.h +0 -0
@@ -80,8 +81,10 @@ struct tls_key_exchange_algorithm {
  						const uint8_t *buf, size_t len);
 
  	ssize_t (*sign)(struct l_tls *tls, uint8_t *out, size_t len,

Can we name this out_len to make it clearer

ell/tls-private.h struct tls_key_exchange_algorithm { · line 84

Quoted 1 line
ell/tls-private.h +1 -0
+			const uint8_t *data, size_t data_len,

These maybe might need to come after the get_hash, since our general pattern is callback, userdata, not the other way around.

ell/tls-private.h struct tls_key_exchange_algorithm { · line 88

Quoted 4 lines
ell/tls-private.h +1 -0
  			tls_get_hash_t get_hash);
  	bool (*verify)(struct l_tls *tls, const uint8_t *in, size_t len,
+			const uint8_t *data, size_t data_len,
  			tls_get_hash_t get_hash);

As above

Quoted 6 lines
ell/tls-private.h +0 -0
  	void (*free_params)(struct l_tls *tls);
ell/tls-suites.c +0 -0
index cb1416a..d3c7f8f 100644

<snip>

ell/tls-suites.c static ssize_t tls_rsa_sign(struct l_tls *tls, uint8_t *out, size_t len, · line 196

Quoted 10 lines
ell/tls-suites.c +4 -2
@@ -188,8 +190,10 @@ static ssize_t tls_rsa_sign(struct l_tls *tls, uint8_t *out, size_t len,
  		*ptr++ = 1;	/* RSA_sign */
  		len -= 2;
  	} else {
-		get_hash(tls, 1, sign_input + 0, NULL, NULL);	/* MD5 */
-		get_hash(tls, 2, sign_input + 16, NULL, NULL);	/* SHA1 */
+		/* MD5 */
+		get_hash(tls, 1, data, data_len, sign_input + 0, NULL, NULL);
+		/* SHA1 */
+		get_hash(tls, 2, data, data_len, sign_input + 16, NULL, NULL);

So for example, here this can be simplified to:

get_hash((tls, L_CHECKSUM_SHA1, data, data_len, sign_input + 16, NULL); etc

Quoted 12 lines
ell/tls-suites.c +1 -0
  		sign_checksum_type = L_CHECKSUM_NONE;
  		sign_input_len = 36;
  	}
@@ -214,6 +218,7 @@ error:
  }
 
  static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len,
+				const uint8_t *data, size_t data_len,
  				tls_get_hash_t get_hash)
  {
  	enum l_checksum_type hash_type;

ell/tls-suites.c static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len, · line 264

Quoted 7 lines
ell/tls-suites.c +2 -2
@@ -255,8 +260,8 @@ static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len,
  			return false;
  		}
 
-		if (!get_hash(tls, in[0], expected, &expected_len,
-				&hash_type)) {
+		if (!get_hash(tls, in[0], data, data_len,
+				expected, &expected_len, &hash_type)) {

And here you'd lookup hash_type first...

ell/tls-suites.c static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len, · line 267

Quoted 3 lines
ell/tls-suites.c +0 -0
  			TLS_DISCONNECT(TLS_ALERT_DECRYPT_ERROR, 0,
  					"Unknown hash type %i", in[0]);
 

The rest seems fine to me.

Regards,
-Denis

#3 lore raw
Quoted 19 lines · Denis Kenzior
Quoted 7 lines · Andrew Zaborowski

This is to illustrate the possible cleanup in DHE to avoid putting a pointer to a stack structure in the handshake state.

---
  ell/tls-private.h |   3 +
  ell/tls-suites.c  | 198 ++++++++++++++++++++--------------------------
  ell/tls.c         |   6 +-
  3 files changed, 91 insertions(+), 116 deletions(-)

So I like this approach way more than the current situation.

Quoted 8 lines
ell/tls-private.h +0 -0
index c82a4d5..134203f 100644
@@ -59,6 +59,7 @@ struct tls_hash_algorithm {
  extern const struct tls_hash_algorithm tls_handshake_hash_data[];
 
Quoted 5 lines

ell/tls-private.h struct tls_hash_algorithm { · line 63

Quoted 3 lines
ell/tls-private.h +1 -0
  typedef bool (*tls_get_hash_t)(struct l_tls *tls, uint8_t tls_id,
+                             const uint8_t *data, size_t data_len,
                              uint8_t *out, size_t *len,

Can we call this out_len to make this clearer?

Ok.

Quoted 9 lines

ell/tls-private.h struct tls_hash_algorithm { · line 64

Quoted 2 lines
ell/tls-private.h +0 -0
                              enum l_checksum_type *type);

So one question I have is whether this type argument is needed. It seems like you only ever use it in 1 place, and pass in NULL everywhere else.

You could simply add a convenience method to lookup l_checksum from a tls_id, no?

Yeah, I was also wondering why I wasn't doing the mapping to the l_checksum_type inside the caller, I'm going to re-check this and possibly convert the signature.

Quoted 8 lines

ell/tls-private.h struct tls_key_exchange_algorithm { · line 83

Quoted 6 lines
ell/tls-private.h +0 -0
@@ -80,8 +81,10 @@ struct tls_key_exchange_algorithm {
                                              const uint8_t *buf, size_t len);
 
      ssize_t (*sign)(struct l_tls *tls, uint8_t *out, size_t len,

Can we name this out_len to make it clearer

Ok.

Quoted 5 lines

ell/tls-private.h struct tls_key_exchange_algorithm { · line 84

Quoted 2 lines
ell/tls-private.h +1 -0
+                     const uint8_t *data, size_t data_len,

These maybe might need to come after the get_hash, since our general pattern is callback, userdata, not the other way around.

Ok I can switch that around although it's a little different from the other userdata's.

Quoted 7 lines

ell/tls-private.h struct tls_key_exchange_algorithm { · line 88

Quoted 5 lines
ell/tls-private.h +1 -0
                      tls_get_hash_t get_hash);
      bool (*verify)(struct l_tls *tls, const uint8_t *in, size_t len,
+                     const uint8_t *data, size_t data_len,
                      tls_get_hash_t get_hash);

As above

Ok.

Best regards