tls: Don't send Client Hello in l_tls_new
PATCH 1/9 tls: Don't send Client Hello in l_tls_new
Give the user time to set up the tls instance's optional parameters -- those that can not be passed as parameters of l_tls_new, before sending the Client Hello message in client mode. We now send the message from l_tls_start which has to be called after the optional setup is done, e.g. set_cacert, set_auth_data, set_debug are called. This way Client Hello can avoid proposing cipher suites that would not have worked if they were negotiated, due to bad certificate type. It also allows us to output debug messages in the Client Hello sending code, and will allow to add methods to define security profiles.
--- ell/ell.sym | 1 + ell/tls-private.h | 1 + ell/tls.c | 48 ++++++++++++++++++++++++++++------------------- ell/tls.h | 3 +++ 4 files changed, 34 insertions(+), 19 deletions(-)
ell/ell.sym +1 -0
@@ -412,6 +412,7 @@ global: l_tls_new; l_tls_free; l_tls_write; + l_tls_start; l_tls_close; l_tls_set_cacert; l_tls_set_auth_data;
ell/tls-private.h +1 -0
@@ -98,6 +98,7 @@ struct tls_compression_method { };enum tls_handshake_state { +TLS_HANDSHAKE_WAIT_START ,TLS_HANDSHAKE_WAIT_HELLO ,TLS_HANDSHAKE_WAIT_CERTIFICATE ,TLS_HANDSHAKE_WAIT_KEY_EXCHANGE ,
ell/tls.c +29 -19
78 lines not on this page · raw message
ell/tls.h +3 -0
@@ -77,6 +77,9 @@ struct l_tls *l_tls_new(bool server, l_tls_write_cb_t app_data_handler,void l_tls_free (struct l_tls *tls); +/* Begin sending connection setup messages to the server */ +bool l_tls_start (struct l_tls *tls); +/* Properly disconnect a connected session */ void l_tls_close (struct l_tls *tls);
Signature · 1 line
2.19.1
PATCH 2/9 unit: Call l_tls_start in tls tests
--- unit/test-tls.c | 3 +++ 1 file changed, 3 insertions(+)
unit/test-tls.c +3 -0
@@ -483,6 +483,9 @@ static void test_tls_test(const void *data)assert (l_tls_set_cacert (s[0 ].tls, test->server_ca_cert_path));assert (l_tls_set_cacert (s[1 ].tls, test->client_ca_cert_path)); +assert (l_tls_start (s[0 ].tls)); +assert (l_tls_start (s[1 ].tls)); +while (1 ) {if (s[0 ].raw_buf_len) { l_tls_handle_rx(s[1].tls, s[0].raw_buf,
Signature · 1 line
2.19.1
PATCH 3/9 tls: Add TLS version number printf macros
In reply to Andrew Zaborowski--- ell/tls-private.h | 3 +++ ell/tls.c | 17 +++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-)
ell/tls-private.h +3 -0
@@ -257,4 +257,7 @@ int tls_parse_certificate_list(const void *data, size_t len,tls_disconnect (tls, desc, local_desc); \ } while (0) +#define TLS_VER_FMT "1.%i" +#define TLS_VER_ARGS (version) (((version) & 0xff) - 1) +const char *tls_handshake_state_to_str (enum tls_handshake_state state);
ell/tls.c +11 -6
@@ -535,14 +535,17 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls,if (suite->encryption && suite->encryption->cipher_type ==TLS_CIPHER_AEAD ) { -if (tls->negotiated_version && - tls->negotiated_version <TLS_V12 ) { +uint16_t negotiated = tls->negotiated_version; + +if (negotiated && negotiated <TLS_V12 ) {if (error) { *error = error_buf;snprintf (error_buf,sizeof (error_buf),"Cipher suite %s uses an AEAD " -"cipher but TLS < 1.2 was " -"negotiated" , suite->name); +"cipher (TLS 1.2+) but " +TLS_VER_FMT " was negotiated" , + suite->name, +TLS_VER_ARGS (negotiated)); }return false;
@@ -1587,7 +1590,8 @@ static void tls_handle_client_hello(struct l_tls *tls,if (i !=HANDSHAKE_HASH_SHA1 && i !=HANDSHAKE_HASH_MD5 )tls_drop_handshake_hash (tls, i); -TLS_DEBUG ("Negotiated TLS 1.%i" , (tls->negotiated_version &0xff ) -1 ); +TLS_DEBUG ("Negotiated TLS " TLS_VER_FMT , +TLS_VER_ARGS (tls->negotiated_version));/* Select a cipher suite according to client's preference list */ while (cipher_suites_size) {
@@ -1715,7 +1719,8 @@ static void tls_handle_server_hello(struct l_tls *tls,if (i !=HANDSHAKE_HASH_SHA1 && i !=HANDSHAKE_HASH_MD5 )tls_drop_handshake_hash (tls, i); -TLS_DEBUG ("Negotiated TLS 1.%i" , (tls->negotiated_version &0xff ) -1 ); +TLS_DEBUG ("Negotiated TLS " TLS_VER_FMT , +TLS_VER_ARGS (tls->negotiated_version));/* Set the new cipher suite and compression method structs */ tls->pending.cipher_suite =tls_find_cipher_suite (cipher_suite_id);
Signature · 1 line
2.19.1
PATCH 4/9 tls: Define and use an enum type for TLS versions
In reply to Andrew Zaborowski--- ell/tls-private.h | 18 ++++++++++-------- ell/tls-record.c | 10 +++++----- ell/tls.c | 36 ++++++++++++++++++------------------ 3 files changed, 33 insertions(+), 31 deletions(-)
ell/tls-private.h +10 -8
@@ -20,13 +20,15 @@* */ -/* Only TLS 1.2 supported */ -#define TLS_V12 ((3 << 8) | 3) -#define TLS_V11 ((3 << 8) | 2) -#define TLS_V10 ((3 << 8) | 1) +enum l_tls_version { +L_TLS_V10 = ((3 <<8 ) |1 ), +L_TLS_V11 = ((3 <<8 ) |2 ), +L_TLS_V12 = ((3 <<8 ) |3 ), +L_TLS_V13 = ((3 <<8 ) |4 ),/* Not supported */ +}; -#define TLS_VERSION TLS_V12 -#define TLS_MIN_VERSION TLS_V10 +#define TLS_VERSION L_TLS_V12 +#define TLS_MIN_VERSION L_TLS_V10enum tls_cipher_type {TLS_CIPHER_STREAM ,
@@ -169,8 +171,8 @@ struct l_tls {struct l_checksum *handshake_hash[__HANDSHAKE_HASH_COUNT];uint8_t prev_digest[__HANDSHAKE_HASH_COUNT][HANDSHAKE_HASH_MAX_SIZE ]; -uint16_t client_version; -uint16_t negotiated_version; +enum l_tls_version client_version; +enum l_tls_version negotiated_version;bool cert_requested, cert_sent;bool peer_authenticated;struct l_cert *peer_cert;
ell/tls-record.c +5 -5
@@ -135,14 +135,14 @@ static void tls_tx_record_plaintext(struct l_tls *tls, offset =0 ; -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) {l_getrandom (ciphertext, tls->record_iv_length[1 ]);l_cipher_set_iv (tls->cipher[1 ], ciphertext, tls->record_iv_length[1 ]); offset = tls->record_iv_length[1 ]; - }else if (tls->negotiated_version >=TLS_V11 ) { + }else if (tls->negotiated_version >=L_TLS_V11 ) {l_getrandom (iv, tls->record_iv_length[1 ]); l_cipher_encrypt(tls->cipher[1], iv, ciphertext,
@@ -444,7 +444,7 @@ static bool tls_handle_ciphertext(struct l_tls *tls)case TLS_CIPHER_BLOCK : i =0 ; -if (tls->negotiated_version >=TLS_V11 ) +if (tls->negotiated_version >=L_TLS_V11 ) i = tls->record_iv_length[0 ];if (fragment_len <= tls->mac_length[0 ] + i) {
@@ -470,7 +470,7 @@ static bool tls_handle_ciphertext(struct l_tls *tls)return false; } -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) {if (!l_cipher_set_iv (tls->cipher[0 ], tls->record_buf +5 , tls->record_iv_length[0 ])) {
@@ -478,7 +478,7 @@ static bool tls_handle_ciphertext(struct l_tls *tls) "Setting fragment IV failed");return false; } - }else if (tls->negotiated_version >=TLS_V11 ) + }else if (tls->negotiated_version >=L_TLS_V11 )if (!l_cipher_decrypt (tls->cipher[0 ], tls->record_buf +5 , iv, tls->record_iv_length[0 ])) {
ell/tls.c +18 -18
@@ -130,7 +130,7 @@ static bool tls_prf_get_bytes(struct l_tls *tls, const void *seed, size_t seed_len, uint8_t *buf, size_t len) { - if (tls->negotiated_version >= TLS_V12) + if (tls->negotiated_version >= L_TLS_V12) return tls12_prf(tls->prf_hmac->l_id, tls->prf_hmac->length, secret, secret_len, label, seed, seed_len, buf, len);
@@ -332,7 +332,7 @@ static bool tls_change_cipher_spec(struct l_tls *tls, bool txrx, key_offset +=2 * enc->key_length; } -if (tls->negotiated_version <=TLS_V10 && +if (tls->negotiated_version <=L_TLS_V10 && tls->cipher_suite[txrx]->encryption && tls->cipher_suite[txrx]->encryption->cipher_type ==TLS_CIPHER_BLOCK ) {
@@ -535,9 +535,9 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls,if (suite->encryption && suite->encryption->cipher_type ==TLS_CIPHER_AEAD ) { -uint16_t negotiated = tls->negotiated_version; +enum l_tls_version negotiated = tls->negotiated_version; -if (negotiated && negotiated <TLS_V12 ) { +if (negotiated && negotiated <L_TLS_V12 ) {if (error) { *error = error_buf; snprintf(error_buf, sizeof(error_buf),
@@ -589,10 +589,10 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls,return false; } - if ((tls->negotiated_version && tls->negotiated_version < TLS_V12 && + if ((tls->negotiated_version && tls->negotiated_version < L_TLS_V12 && (!l_checksum_is_supported(L_CHECKSUM_MD5, true) || !l_checksum_is_supported(L_CHECKSUM_SHA1, true))) || - (tls->negotiated_version >= TLS_V12 && + (tls->negotiated_version >= L_TLS_V12 && !l_checksum_is_supported( suite->prf_hmac != L_CHECKSUM_NONE ? suite->prf_hmac : L_CHECKSUM_SHA256,
@@ -1034,7 +1034,7 @@ static bool tls_send_certificate_request(struct l_tls *tls)* affect both of these steps so revisit which set we're passing * here. */ -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) { signature_hash_ptr = ptr; ptr +=2 ;
@@ -1112,7 +1112,7 @@ static void tls_generate_master_secret(struct l_tls *tls, tls->pending.cipher_suite->mac->mac_length;if (tls->pending.cipher_suite->encryption && - tls->negotiated_version <=TLS_V10 && + tls->negotiated_version <=L_TLS_V10 && tls->pending.cipher_suite->encryption->cipher_type ==TLS_CIPHER_BLOCK ) key_block_size += 2 *
@@ -1198,7 +1198,7 @@ static ssize_t tls_rsa_sign(struct l_tls *tls, uint8_t *out, size_t len,return -ENOKEY ; } -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) {const struct tls_hash_algorithm *hash_type = &tls_handshake_hash_data[tls->signature_hash];
@@ -1250,7 +1250,7 @@ static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len,/* 2 bytes for SignatureAndHashAlgorithm if version >= 1.2 */ offset =2 ; -if (tls->negotiated_version <TLS_V12 ) +if (tls->negotiated_version <L_TLS_V12 ) offset =0 ; if (len < offset + 2 ||
@@ -1271,7 +1271,7 @@ static bool tls_rsa_verify(struct l_tls *tls, const uint8_t *in, size_t len,return false; } -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) {/* Only RSA supported */ if (in[1 ] !=1 /* RSA_sign */ ) { TLS_DISCONNECT(TLS_ALERT_DECRYPT_ERROR, 0,
@@ -1392,7 +1392,7 @@ static bool tls_send_certificate_verify(struct l_tls *tls)return false;/* Stop maintaining handshake message hashes other than the PRF hash */ -if (tls->negotiated_version >=TLS_V12 ) +if (tls->negotiated_version >=L_TLS_V12 )for (i =0 ; i < __HANDSHAKE_HASH_COUNT; i++)if (&tls_handshake_hash_data[i] != tls->prf_hmac)tls_drop_handshake_hash (tls, i);
@@ -1417,7 +1417,7 @@ static void tls_send_finished(struct l_tls *tls)uint8_t seed[HANDSHAKE_HASH_MAX_SIZE *2 ];size_t seed_len; -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) {/* Same hash type as that used for the PRF (usually SHA256) */ enum handshake_hash_type hash;
@@ -1458,7 +1458,7 @@ static bool tls_verify_finished(struct l_tls *tls, const uint8_t *received,return false; } -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) {enum handshake_hash_type hash;for (hash =0 ; hash < __HANDSHAKE_HASH_COUNT; hash++)
@@ -1585,7 +1585,7 @@ static void tls_handle_client_hello(struct l_tls *tls, TLS_VERSION : tls->client_version;/* Stop maintaining handshake message hashes other than MD1 and SHA. */ -if (tls->negotiated_version <TLS_V12 ) +if (tls->negotiated_version <L_TLS_V12 )for (i =0 ; i < __HANDSHAKE_HASH_COUNT; i++)if (i !=HANDSHAKE_HASH_SHA1 && i !=HANDSHAKE_HASH_MD5 )tls_drop_handshake_hash (tls, i);
@@ -1714,7 +1714,7 @@ static void tls_handle_server_hello(struct l_tls *tls, }/* Stop maintaining handshake message hashes other than MD1 and SHA. */ -if (tls->negotiated_version <TLS_V12 ) +if (tls->negotiated_version <L_TLS_V12 )for (i =0 ; i < __HANDSHAKE_HASH_COUNT; i++)if (i !=HANDSHAKE_HASH_SHA1 && i !=HANDSHAKE_HASH_MD5 )tls_drop_handshake_hash (tls, i);
@@ -1911,7 +1911,7 @@ static void tls_handle_certificate_request(struct l_tls *tls,* lists for use in tls_send_certificate. */ -if (tls->negotiated_version >=TLS_V12 ) { +if (tls->negotiated_version >=L_TLS_V12 ) {/* * This only makes sense as a variable-length field, assume * there's a typo in RFC5246 7.4.4 here.
@@ -2125,7 +2125,7 @@ static void tls_handle_certificate_verify(struct l_tls *tls,return ;/* Stop maintaining handshake message hashes other than the PRF hash */ -if (tls->negotiated_version >=TLS_V12 ) +if (tls->negotiated_version >=L_TLS_V12 )for (i =0 ; i < __HANDSHAKE_HASH_COUNT; i++)if (&tls_handshake_hash_data[i] != tls->prf_hmac)tls_drop_handshake_hash (tls, i);
Signature · 1 line
2.19.1
PATCH 5/9 tls: Implement l_tls_set_version_range
In reply to Andrew ZaborowskiAllow user to set custom min and max TLS version limits, this can be used to ensure we comply with a specific security profile.
--- ell/ell.sym | 1 + ell/tls-private.h | 10 ++----- ell/tls-record.c | 4 +-- ell/tls.c | 74 +++++++++++++++++++++++++++++++++++++---------- ell/tls.h | 11 ++++++- 5 files changed, 73 insertions(+), 27 deletions(-)
ell/ell.sym +1 -0
@@ -416,6 +416,7 @@ global: l_tls_close; l_tls_set_cacert; l_tls_set_auth_data; + l_tls_set_version_range; l_tls_alert_to_str; l_tls_set_debug; /* uintset */
ell/tls-private.h +2 -8
@@ -20,14 +20,7 @@* */ -enum l_tls_version { -L_TLS_V10 = ((3 <<8 ) |1 ), -L_TLS_V11 = ((3 <<8 ) |2 ), -L_TLS_V12 = ((3 <<8 ) |3 ), -L_TLS_V13 = ((3 <<8 ) |4 ),/* Not supported */ -}; - -#define TLS_VERSION L_TLS_V12 +#define TLS_MAX_VERSION L_TLS_V12#define TLS_MIN_VERSION L_TLS_V10enum tls_cipher_type {
@@ -147,6 +140,7 @@ struct l_tls {l_tls_debug_cb_t debug_handler;l_tls_destroy_cb_t debug_destroy;void *debug_data; +enum l_tls_version min_version, max_version;struct l_queue *ca_certs;struct l_certchain *cert;
ell/tls-record.c +2 -2
@@ -223,7 +223,7 @@ void tls_tx_record(struct l_tls *tls, enum tls_content_type type, TX_RECORD_TAILROOM];uint8_t *fragment, *plaintext;uint16_t fragment_len; -uint16_t version = tls->negotiated_version ?:TLS_MIN_VERSION ; +uint16_t version = tls->negotiated_version ?: tls->min_version;if (type ==TLS_CT_ALERT ) tls->record_flush = true;
@@ -393,7 +393,7 @@ static bool tls_handle_ciphertext(struct l_tls *tls)if ((tls->negotiated_version && tls->negotiated_version != version) || (!tls->negotiated_version && - tls->record_buf[1 ] !=0x03 /* Appending E.1 */ )) { + tls->record_buf[1 ] !=0x03 /* Appendix E.1 */ )) {TLS_DISCONNECT (TLS_ALERT_PROTOCOL_VERSION ,0 ,"Record version mismatch: %02x" , version);return false;
ell/tls.c +58 -16
151 lines not on this page · raw message
ell/tls.h +10 -1
@@ -25,6 +25,13 @@ extern "C" { #endif +enum l_tls_version { + L_TLS_V10 = ((3 << 8) | 1), + L_TLS_V11 = ((3 << 8) | 2), + L_TLS_V12 = ((3 << 8) | 3), + L_TLS_V13 = ((3 << 8) | 4), /* Not supported */ +}; + struct l_tls; enum l_tls_alert_desc {
@@ -63,7 +70,6 @@ typedef void (*l_tls_disconnect_cb_t)(enum l_tls_alert_desc reason,typedef void (*l_tls_debug_cb_t )(const char *str,void *user_data);typedef void (*l_tls_destroy_cb_t )(void *user_data); -/* * app_data_handler gets called with newly received decrypted data. * tx_handler gets called to send TLS payloads off to remote end.
@@ -107,6 +113,9 @@ bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path,const char *priv_key_path, const char *priv_key_passphrase); +void l_tls_set_version_range (struct l_tls *tls, +uint16_t min_version,uint16_t max_version); +const char *l_tls_alert_to_str (enum l_tls_alert_desc desc);enum l_checksum_type ;
Signature · 1 line
2.19.1
Re: [PATCH 5/9] tls: Implement l_tls_set_version_range
Hi Andrew,
Quoted 10 lines · Andrew Zaborowski
Allow user to set custom min and max TLS version limits, this can be used to ensure we comply with a specific security profile.
--- ell/ell.sym | 1 + ell/tls-private.h | 10 ++----- ell/tls-record.c | 4 +-- ell/tls.c | 74 +++++++++++++++++++++++++++++++++++++---------- ell/tls.h | 11 ++++++- 5 files changed, 73 insertions(+), 27 deletions(-)
<snip>
Quoted 8 lines
ell/tls-private.h +0
-0@@ -20,14 +20,7 @@* */
ell/tls-private.h line 24
Quoted 10 lines
ell/tls-private.h +1
-8-enum l_tls_version { -L_TLS_V10 = ((3 <<8 ) |1 ), -L_TLS_V11 = ((3 <<8 ) |2 ), -L_TLS_V12 = ((3 <<8 ) |3 ), -L_TLS_V13 = ((3 <<8 ) |4 ),/* Not supported */ -}; - -#define TLS_VERSION L_TLS_V12 +#define TLS_MAX_VERSION L_TLS_V12#define TLS_MIN_VERSION L_TLS_V10
So we generally discourage #defines that are used only once. Are you planning to use this outside l_tls_new? This may also be relevant for TLS_MIN_VERSION since you are getting rid of many of its usages as well.
ell/tls-private.h struct l_tls { · line 143
Quoted 7 lines
ell/tls-private.h +1
-0enum tls_cipher_type {@@ -147,6 +140,7 @@ struct l_tls {l_tls_debug_cb_t debug_handler;l_tls_destroy_cb_t debug_destroy;void *debug_data; +enum l_tls_version min_version, max_version;
Separate lines please :)
Quoted 3 lines
ell/tls-private.h +0
-0struct l_queue *ca_certs;struct l_certchain *cert;
<snip>
ell/tls-private.h static bool tls_handle_ciphertext(struct l_tls *tls) · line 396
Quoted 6 lines
ell/tls-private.h +1
-1@@ -393,7 +393,7 @@ static bool tls_handle_ciphertext(struct l_tls *tls)if ((tls->negotiated_version && tls->negotiated_version != version) || (!tls->negotiated_version && - tls->record_buf[1 ] !=0x03 /* Appending E.1 */ )) { + tls->record_buf[1 ] !=0x03 /* Appendix E.1 */ )) {
Separate patch please
Quoted 3 lines
ell/tls-private.h +0
-0TLS_DISCONNECT (TLS_ALERT_PROTOCOL_VERSION ,0 ,"Record version mismatch: %02x" , version);return false;
<snip>
ell/tls-private.h static bool tls_cipher_suite_is_compatible(struct l_tls *tls, · line 608
Quoted 11 lines
ell/tls-private.h +3
-4@@ -589,10 +603,13 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls,return false; } - if ((tls->negotiated_version && tls->negotiated_version < L_TLS_V12 && - (!l_checksum_is_supported(L_CHECKSUM_MD5, true) || - !l_checksum_is_supported(L_CHECKSUM_SHA1, true))) || - (tls->negotiated_version >= L_TLS_V12 && + if ( + ((tls->max_version < L_TLS_V12 || + (negotiated && negotiated < L_TLS_V12)) &&
So the previous version of this only looked at the negotiated version, but this one also looks@the max version. Should this function take the TLS version as an argument and let the caller figure out the policy?
ell/tls-private.h static bool tls_cipher_suite_is_compatible(struct l_tls *tls, · line 615
Quoted 7 lines
ell/tls-private.h +4
-0+ (!l_checksum_is_supported(L_CHECKSUM_MD5, true) || + !l_checksum_is_supported(L_CHECKSUM_SHA1, true))) || + ((tls->min_version >= L_TLS_V12 || + negotiated >= L_TLS_V12) && !l_checksum_is_supported( suite->prf_hmac != L_CHECKSUM_NONE ? suite->prf_hmac : L_CHECKSUM_SHA256,
Why is this indented so far to the right? But really, can we put this into a separate function and make this a bit more readable, maybe with a few comments?
ell/tls-private.h LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path, · line 2786
Quoted 7 lines
ell/tls-private.h +3
-0@@ -2751,6 +2781,18 @@ LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path,return true; } +LIB_EXPORT void l_tls_set_version_range(struct l_tls *tls, +uint16_t min_version, +uint16_t max_version)
Why uint16?
ell/tls-private.h LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path, · line 2793
Quoted 7 lines
ell/tls-private.h +7
-0+{ + tls->min_version = + (min_version && min_version >TLS_MIN_VERSION ) ? + min_version :TLS_MIN_VERSION ; + tls->max_version = + (max_version && max_version <TLS_MAX_VERSION ) ? + max_version :TLS_MAX_VERSION ;
Yikes. Again, why not use enums here and skip these checks? If someone wants to pass arbitrary int values to this function, that is their problem.
ell/tls-private.h LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path, · line 2798
Quoted 5 lines
ell/tls-private.h +2
-0+} + LIB_EXPORT const char *l_tls_alert_to_str(enum l_tls_alert_desc desc) {switch (desc) {
Regards,
-Denis
Re: [PATCH 5/9] tls: Implement l_tls_set_version_range
HI Denis,
Quoted 22 lines · Denis Kenzior
Quoted 10 lines · Andrew Zaborowski
Allow user to set custom min and max TLS version limits, this can be used to ensure we comply with a specific security profile.
--- ell/ell.sym | 1 + ell/tls-private.h | 10 ++----- ell/tls-record.c | 4 +-- ell/tls.c | 74 +++++++++++++++++++++++++++++++++++++---------- ell/tls.h | 11 ++++++- 5 files changed, 73 insertions(+), 27 deletions(-)<snip>
Quoted 8 lines
ell/tls-private.h +0
-0@@ -20,14 +20,7 @@* */
Quoted 14 lines
ell/tls-private.h line 24
Quoted 10 lines
ell/tls-private.h +1
-8-enum l_tls_version { -L_TLS_V10 = ((3 <<8 ) |1 ), -L_TLS_V11 = ((3 <<8 ) |2 ), -L_TLS_V12 = ((3 <<8 ) |3 ), -L_TLS_V13 = ((3 <<8 ) |4 ),/* Not supported */ -}; - -#define TLS_VERSION L_TLS_V12 +#define TLS_MAX_VERSION L_TLS_V12#define TLS_MIN_VERSION L_TLS_V10So we generally discourage #defines that are used only once. Are you planning to use this outside
l_tls_new? This may also be relevant forTLS_MIN_VERSIONsince you are getting rid of many of its usages as well.
Other than l_tls_set_version_range no (although for 1.3 we potentially may need to move it back to tls-private.h because version negotiation happens in an extension)
Quoted 10 lines
ell/tls-private.h struct l_tls { · line 143
Quoted 8 lines
ell/tls-private.h +1
-0enum tls_cipher_type {@@ -147,6 +140,7 @@ struct l_tls {l_tls_debug_cb_t debug_handler;l_tls_destroy_cb_t debug_destroy;void *debug_data; +enum l_tls_version min_version, max_version;Separate lines please :)
Okay.
Quoted 26 lines
Quoted 4 lines
ell/tls-private.h +0
-0struct l_queue *ca_certs;struct l_certchain *cert;<snip>
ell/tls-private.h static bool tls_handle_ciphertext(struct l_tls *tls) · line 396
Quoted 6 lines
ell/tls-private.h +1
-1@@ -393,7 +393,7 @@ static bool tls_handle_ciphertext(struct l_tls *tls)if ((tls->negotiated_version && tls->negotiated_version != version) || (!tls->negotiated_version && - tls->record_buf[1 ] !=0x03 /* Appending E.1 */ )) { + tls->record_buf[1 ] !=0x03 /* Appendix E.1 */ )) {Separate patch please
Quoted 3 lines
ell/tls-private.h +0
-0TLS_DISCONNECT (TLS_ALERT_PROTOCOL_VERSION ,0 ,"Record version mismatch: %02x" , version);return false;<snip>
Quoted 4 lines
ell/tls-private.h +0
-0@@ -589,10 +603,13 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls,return false; }
Quoted 11 lines
ell/tls-private.h static bool tls_cipher_suite_is_compatible(struct l_tls *tls, · line 608
Quoted 7 lines
ell/tls-private.h +3
-4- if ((tls->negotiated_version && tls->negotiated_version < L_TLS_V12 && - (!l_checksum_is_supported(L_CHECKSUM_MD5, true) || - !l_checksum_is_supported(L_CHECKSUM_SHA1, true))) || - (tls->negotiated_version >= L_TLS_V12 && + if ( + ((tls->max_version < L_TLS_V12 || + (negotiated && negotiated < L_TLS_V12)) &&So the previous version of this only looked at the negotiated version, but this one also looks at the max version. Should this function take the TLS version as an argument and let the caller figure out the policy?
So the idea here is that we can eliminate some suites before negotiated_version is set but we're dealing with an interval so we have a min and a max. I guess we can simplify it locally (so as not to complicate it for the callers) by setting min and max to either tls->min/max_version or tls->negotiated_version.
Quoted 10 lines
ell/tls-private.h static bool tls_cipher_suite_is_compatible(struct l_tls *tls, · line 615
Quoted 8 lines
ell/tls-private.h +4
-0+ (!l_checksum_is_supported(L_CHECKSUM_MD5, true) || + !l_checksum_is_supported(L_CHECKSUM_SHA1, true))) || + ((tls->min_version >= L_TLS_V12 || + negotiated >= L_TLS_V12) && !l_checksum_is_supported( suite->prf_hmac != L_CHECKSUM_NONE ? suite->prf_hmac : L_CHECKSUM_SHA256,Why is this indented so far to the right?
In an attempt to comply with coding-style.txt :) I'm going with the editor's auto indent for the other lines where we have no specific rules.
Quoted 3 lines
But really, can we put this
into a separate function and make this a bit more readable, maybe with a few comments?
Ok.
Quoted 19 lines
ell/tls-private.h LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path, · line 2786
Quoted 8 lines
ell/tls-private.h +3
-0@@ -2751,6 +2781,18 @@ LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path,return true; } +LIB_EXPORT void l_tls_set_version_range(struct l_tls *tls, +uint16_t min_version, +uint16_t max_version)Why uint16?
ell/tls-private.h LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path, · line 2793
Quoted 7 lines
ell/tls-private.h +7
-0+{ + tls->min_version = + (min_version && min_version >TLS_MIN_VERSION ) ? + min_version :TLS_MIN_VERSION ; + tls->max_version = + (max_version && max_version <TLS_MAX_VERSION ) ? + max_version :TLS_MAX_VERSION ;
Quoted 2 lines
Yikes. Again, why not use enums here and skip these checks? If someone wants to pass arbitrary int values to this function, that is their problem.
Oops so I was determined to use enum l_tls_version for the parameters but somehow screwed up a rebase I think. Still I think these checks are useful and we should accept possible other values.
With both l_tls_set_version_range and l_tls_set_cipher_suites I think it's important that the caller does not have to worry about which versions and suites we actually support internally. They should be able to say "for this instance the policy is ok with anything from SSL 2.0 to TLS 1.1 and such and such cipher suites" without diving into our .c files. On the other hand our code can trivially limit this to the features that are actually usable so we don't end up negotiating a version we don't support.
Best regards
PATCH 6/9 unit: Test TLS 1.0, 1.1 and 1.2
In reply to Andrew Zaborowski--- unit/test-tls.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
unit/test-tls.c +20 -1
@@ -431,7 +431,8 @@ static void tls_debug_cb(const char *str, void *user_data)l_info ("%s %s" , (const char *) user_data, str); } -static void test_tls_test (const void *data) +static void test_tls_with_ver (const void *data, +uint16_t server_ver,uint16_t client_ver) {bool auth_ok;const struct tls_conn_test *test = data;
@@ -466,6 +467,12 @@ static void test_tls_test(const void *data)assert (s[0 ].tls);assert (s[1 ].tls); +if (server_ver) +l_tls_set_version_range (s[0 ].tls, server_ver, server_ver); + +if (client_ver) +l_tls_set_version_range (s[1 ].tls, client_ver, client_ver); +if (getenv ("TLS_SERVER_DEBUG" ))l_tls_set_debug (s[0 ].tls, tls_debug_cb,"server" ,NULL );
@@ -505,6 +512,18 @@ static void test_tls_test(const void *data)l_tls_free (s[1 ].tls); } +static void test_tls_test (const void *data) +{ +/* +* 1.2 should get negotiated in the first case. If the three +* scenarios succeed that's already good but can be checked with: +* $ TLS_DEBUG=1 unit/test-tls 2>&1 | grep "Negotiated" +*/ +test_tls_with_ver (data,0 ,0 ); +test_tls_with_ver (data,0 ,L_TLS_V11 ); +test_tls_with_ver (data,L_TLS_V10 ,0 ); +} +int main (int argc,char *argv[]) {l_test_init (&argc, &argv);
Signature · 1 line
2.19.1
PATCH 7/9 tls: Allow user to set custom list of cipher suites
In reply to Andrew ZaborowskiTLS "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
@@ -147,6 +147,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;
@@ -233,6 +235,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
PATCH 8/9 fixme: add mte to the set ci su patch
In reply to Andrew Zaborowski--- ell/tls.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
ell/tls.c +7 -7
@@ -1625,7 +1625,7 @@ static void tls_handle_client_hello(struct l_tls *tls,TLS_DEBUG ("Negotiated TLS " TLS_VER_FMT ,TLS_VER_ARGS (tls->negotiated_version)); -if (tls->cipher_suite_pref_list) { +if (!tls->cipher_suite_pref_list) {TLS_DISCONNECT (TLS_ALERT_INTERNAL_ERROR ,0 ,"No usable cipher suites" );return ;
@@ -2846,10 +2846,10 @@ LIB_EXPORT void l_tls_set_version_range(struct l_tls *tls,bool tls_set_cipher_suites (struct l_tls *tls,const char **suite_list) { -unsigned int len;struct tls_cipher_suite **suite; -l_free (tls->cipher_suite_pref_list); +if (tls->cipher_suite_pref_list != tls_cipher_suite_pref) +l_free (tls->cipher_suite_pref_list);if (!suite_list) {/* Use our default cipher suite preference list */
@@ -2857,19 +2857,19 @@ bool tls_set_cipher_suites(struct l_tls *tls, const char **suite_list)return true; } - len =l_strv_length ((char **) suite_list); - tls->cipher_suite_pref_list = l_new(struct tls_cipher_suite *, len + 1); + tls->cipher_suite_pref_list = l_new(struct tls_cipher_suite *, +l_strv_length ((char **) suite_list) +1 ); suite = tls->cipher_suite_pref_list;for (; *suite_list; suite_list++) {unsigned int i; -for (i =0 ; i < len; i++) +for (i =0 ; i <L_ARRAY_SIZE (tls_cipher_suite_pref); i++)if (!strcmp (tls_cipher_suite_pref[i]->name, *suite_list))break ; -if (i < len) +if (i <L_ARRAY_SIZE (tls_cipher_suite_pref)) *suite++ = tls_cipher_suite_pref[i];else TLS_DEBUG("Cipher suite %s is not supported",
Signature · 1 line
2.19.1
Re: [PATCH 8/9] fixme: add mte to the set ci su patch
Hi Andrew,
Quoted 4 lines · Andrew Zaborowski
--- ell/tls.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
??? :)
Regards,
-Denis
Re: [PATCH 8/9] fixme: add mte to the set ci su patch
Hi Denis,
Quoted 7 lines · Denis Kenzior
Quoted 4 lines · Andrew Zaborowski
--- ell/tls.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)??? :)
Somehow I sent out a temporary commit that was supposed to go away in the rebase, sorry.
Best regards
PATCH 9/9 unit: Test many TLS cipher suite and version combinations
In reply to Andrew Zaborowski--- unit/test-tls.c | 131 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 8 deletions(-)
unit/test-tls.c +123 -8
@@ -302,11 +302,16 @@ struct tls_conn_test {const char *server_key_passphrase;const char *server_ca_cert_path;const char *server_expect_identity; +const char **server_cipher_suites;const char *client_cert_path;const char *client_key_path;const char *client_key_passphrase;const char *client_ca_cert_path;const char *client_expect_identity; +const char **client_cipher_suites; +bool expect_alert; +bool expect_client_start_fail; +enum l_tls_alert_desc alert_desc; };static const struct tls_conn_test tls_conn_test_no_auth = {
@@ -362,6 +367,49 @@ static const struct tls_conn_test tls_conn_test_full_auth = { .client_expect_identity = "Foo Example Organization", }; +static const struct tls_conn_test tls_conn_test_bad_client_suite = { + .server_cert_path =CERTDIR "cert-server.pem" , + .server_key_path =CERTDIR "cert-server-key-pkcs8.pem" , + .server_ca_cert_path =CERTDIR "cert-ca.pem" , + .server_expect_identity ="Bar Example Organization" , + .client_cert_path =CERTDIR "cert-client.pem" , + .client_key_path =CERTDIR "cert-client-key-pkcs8.pem" , + .client_ca_cert_path =CERTDIR "cert-ca.pem" , + .client_expect_identity ="Foo Example Organization" , + .client_cipher_suites = (const char *[]) {"UNKNOWN" ,NULL }, + .expect_client_start_fail = true, +}; + +static const struct tls_conn_test tls_conn_test_suite_mismatch = { + .server_cert_path =CERTDIR "cert-server.pem" , + .server_key_path =CERTDIR "cert-server-key-pkcs8.pem" , + .server_ca_cert_path =CERTDIR "cert-ca.pem" , + .server_expect_identity ="Bar Example Organization" , + .server_cipher_suites = + (const char *[]) {"TLS_RSA_WITH_AES_128_CBC_SHA256" ,NULL }, + .client_cert_path =CERTDIR "cert-client.pem" , + .client_key_path =CERTDIR "cert-client-key-pkcs8.pem" , + .client_ca_cert_path =CERTDIR "cert-ca.pem" , + .client_expect_identity ="Foo Example Organization" , + .client_cipher_suites = + (const char *[]) {"TLS_RSA_WITH_AES_256_CBC_SHA256" ,NULL }, + .expect_alert = true, + .alert_desc =TLS_ALERT_INSUFFICIENT_SECURITY , +}; + +static const struct tls_conn_test tls_conn_test_version_mismatch = { + .server_cert_path =CERTDIR "cert-server.pem" , + .server_key_path =CERTDIR "cert-server-key-pkcs8.pem" , + .server_ca_cert_path =CERTDIR "cert-ca.pem" , + .server_expect_identity ="Bar Example Organization" , + .client_cert_path =CERTDIR "cert-client.pem" , + .client_key_path =CERTDIR "cert-client-key-pkcs8.pem" , + .client_ca_cert_path =CERTDIR "cert-ca.pem" , + .client_expect_identity ="Foo Example Organization" , + .expect_alert = true, + .alert_desc =TLS_ALERT_PROTOCOL_VERSION , +}; +#define identity_compare (a, b) ((!(a) && !(b)) || ((a) && (b) && !strcmp(a, b)))struct tls_test_state {
@@ -378,6 +426,9 @@ struct tls_test_state {const char *send_data;const char *expect_data;const char *expect_peer; + +bool expect_alert; +enum l_tls_alert_desc alert_desc; };static void tls_test_new_data (const uint8_t *data,size_t len,void *user_data)
@@ -411,6 +462,7 @@ static void tls_test_ready(const char *peer_identity, void *user_data)struct tls_test_state *s = user_data;assert (!s->ready); +assert (!s->expect_alert);assert ((!s->expect_peer && !peer_identity) || (s->expect_peer && peer_identity && !strcmp (s->expect_peer, peer_identity)));
@@ -423,7 +475,11 @@ static void tls_test_ready(const char *peer_identity, void *user_data)static void tls_test_disconnected (enum l_tls_alert_desc reason,bool remote,void *user_data) { -assert (false); +struct tls_test_state *s = user_data; + +assert (s->expect_alert); +assert (reason == s->alert_desc); + s->success = true; }static void tls_debug_cb (const char *str,void *user_data)
@@ -431,11 +487,10 @@ static void tls_debug_cb(const char *str, void *user_data)l_info ("%s %s" , (const char *) user_data, str); } -static void test_tls_with_ver (const void *data, +static void test_tls_with_ver (const struct tls_conn_test *test,uint16_t server_ver,uint16_t client_ver) {bool auth_ok; -const struct tls_conn_test *test = data;struct tls_test_state s[2 ] = { { .ready = false,
@@ -445,6 +500,8 @@ static void test_tls_with_ver(const void *data, .send_data = "server to client", .expect_data = "client to server", .expect_peer = test->server_expect_identity, + .expect_alert = test->expect_alert, + .alert_desc = test->alert_desc, }, { .ready = false,
@@ -454,6 +511,8 @@ static void test_tls_with_ver(const void *data, .send_data = "client to server", .expect_data = "server to client", .expect_peer = test->client_expect_identity, + .expect_alert = test->expect_alert, + .alert_desc = test->alert_desc, }, };
@@ -473,6 +532,12 @@ static void test_tls_with_ver(const void *data,if (client_ver)l_tls_set_version_range (s[1 ].tls, client_ver, client_ver); +if (test->server_cipher_suites) +tls_set_cipher_suites (s[0 ].tls, test->server_cipher_suites); + +if (test->client_cipher_suites) +tls_set_cipher_suites (s[1 ].tls, test->client_cipher_suites); +if (getenv ("TLS_SERVER_DEBUG" ))l_tls_set_debug (s[0 ].tls, tls_debug_cb,"server" ,NULL );
@@ -491,9 +556,12 @@ static void test_tls_with_ver(const void *data,assert (l_tls_set_cacert (s[1 ].tls, test->client_ca_cert_path));assert (l_tls_start (s[0 ].tls)); -assert (l_tls_start (s[1 ].tls)); +assert (!!l_tls_start (s[1 ].tls) == !test->expect_client_start_fail); -while (1 ) { +if (test->expect_client_start_fail) + goto done; + +while (!(test->expect_alert && s[0 ].success && s[1 ].success)) {if (s[0 ].raw_buf_len) {l_tls_handle_rx (s[1 ].tls, s[0 ].raw_buf, s[0 ].raw_buf_len);
@@ -508,24 +576,60 @@ static void test_tls_with_ver(const void *data,assert (s[0 ].success && s[1 ].success); +done:l_tls_free (s[0 ].tls);l_tls_free (s[1 ].tls); }static void test_tls_test (const void *data) { +const struct tls_conn_test *test = data; +/* * 1.2 should get negotiated in the first case. If the three * scenarios succeed that's already good but can be checked with: * $ TLS_DEBUG=1 unit/test-tls 2>&1 | grep "Negotiated" */ -test_tls_with_ver (data,0 ,0 ); -test_tls_with_ver (data,0 ,L_TLS_V11 ); -test_tls_with_ver (data,L_TLS_V10 ,0 ); +test_tls_with_ver (test,0 ,0 ); +test_tls_with_ver (test,0 ,L_TLS_V11 ); +test_tls_with_ver (test,L_TLS_V10 ,0 ); +} + +static void test_tls_version_mismatch_test (const void *data) +{ +test_tls_with_ver (&tls_conn_test_version_mismatch, +L_TLS_V11 ,L_TLS_V12 ); +test_tls_with_ver (&tls_conn_test_version_mismatch, +L_TLS_V12 ,L_TLS_V10 ); +test_tls_with_ver (&tls_conn_test_version_mismatch, +L_TLS_V10 ,L_TLS_V11 ); +} + +static void test_tls_suite_test (const void *data) +{ +const char *suite_name = data; +const char *client_cipher_suites[] = { suite_name,NULL }; +struct tls_conn_test test = tls_conn_test_full_auth; + + test.client_cipher_suites = client_cipher_suites; +test_tls_with_ver (&test,0 ,0 ); }int main (int argc,char *argv[]) { +const char *suite_names[] = { +"TLS_RSA_WITH_RC4_128_MD5" , +"TLS_RSA_WITH_RC4_128_SHA" , +"TLS_RSA_WITH_3DES_EDE_CBC_SHA" , +"TLS_RSA_WITH_AES_128_CBC_SHA" , +"TLS_RSA_WITH_AES_256_CBC_SHA" , +"TLS_RSA_WITH_AES_128_CBC_SHA256" , +"TLS_RSA_WITH_AES_256_CBC_SHA256" , +"TLS_RSA_WITH_AES_128_GCM_SHA256" , +"TLS_RSA_WITH_AES_256_GCM_SHA384" , +NULL + }, **name; +l_test_init (&argc, &argv); if (!l_checksum_is_supported(L_CHECKSUM_MD5, false) ||
@@ -584,6 +688,17 @@ int main(int argc, char *argv[])l_test_add ("TLS connection full auth" , test_tls_test, &tls_conn_test_full_auth); +l_test_add ("TLS connection bad client cipher suite" , test_tls_test, + &tls_conn_test_bad_client_suite); +l_test_add ("TLS connection cipher suite mismatch" , test_tls_test, + &tls_conn_test_suite_mismatch); + +l_test_add ("TLS connection version mismatch" , + test_tls_version_mismatch_test,NULL ); + +for (name = suite_names; *name; name++) +l_test_add (*name, test_tls_suite_test, *name); + done:return l_test_run (); }
Signature · 1 line
2.19.1
Hi Andrew,
Quoted 9 lines · Andrew Zaborowski
Give the user time to set up the tls instance's optional parameters -- those that can not be passed as parameters of
l_tls_new, before sending the Client Hello message in client mode. We now send the message froml_tls_startwhich has to be called after the optional setup is done, e.g.set_cacert,set_auth_data,set_debugare called. This way Client Hello can avoid proposing cipher suites that would not have worked if they were negotiated, due to bad certificate type. It also allows us to output debug messages in the Client Hello sending code, and will allow to add methods to define security profiles.
Quoted 6 lines
--- ell/ell.sym | 1 + ell/tls-private.h | 1 + ell/tls.c | 48 ++++++++++++++++++++++++++++------------------- ell/tls.h | 3 +++ 4 files changed, 34 insertions(+), 19 deletions(-)
Patches 1-4 applied, thanks.
Regards,
-Denis