]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[tls] Generate key material after sending ClientKeyExchange
authorMichael Brown <mcb30@ipxe.org>
Tue, 30 Jan 2024 15:17:49 +0000 (15:17 +0000)
committerMichael Brown <mcb30@ipxe.org>
Tue, 30 Jan 2024 15:25:38 +0000 (15:25 +0000)
The construction of the key material for the pending cipher suites
from the TLS master secret must happen regardless of which key
exchange algorithm is in use, and the key material is not required to
send the ClientKeyExchange handshake (which is sent before changing
cipher suites).

Centralise the call to tls_generate_keys() after performing key
exchange via the selected algorithm.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/net/tls.c

index 7f31c8f78bbcc6b8a97224ba647ea329930d83f6..da16889fd3399efab8a22f3da582d5be39fe410e 100644 (file)
@@ -1363,13 +1363,6 @@ static int tls_send_client_key_exchange_pubkey ( struct tls_connection *tls ) {
        tls_generate_master_secret ( tls, &pre_master_secret,
                                     sizeof ( pre_master_secret ) );
 
-       /* Generate keys */
-       if ( ( rc = tls_generate_keys ( tls ) ) != 0 ) {
-               DBGC ( tls, "TLS %p could not generate keys: %s\n",
-                      tls, strerror ( rc ) );
-               return rc;
-       }
-
        /* Encrypt pre-master secret using server's public key */
        memset ( &key_xchg, 0, sizeof ( key_xchg ) );
        len = pubkey_encrypt ( pubkey, cipherspec->pubkey_ctx,
@@ -1567,13 +1560,6 @@ static int tls_send_client_key_exchange_dhe ( struct tls_connection *tls ) {
                /* Generate master secret */
                tls_generate_master_secret ( tls, pre_master_secret, len );
 
-               /* Generate keys */
-               if ( ( rc = tls_generate_keys ( tls ) ) != 0 ) {
-                       DBGC ( tls, "TLS %p could not generate keys: %s\n",
-                              tls, strerror ( rc ) );
-                       goto err_generate_keys;
-               }
-
                /* Transmit Client Key Exchange record */
                if ( ( rc = tls_send_handshake ( tls, key_xchg,
                                                 sizeof ( *key_xchg ) ) ) !=0){
@@ -1581,7 +1567,6 @@ static int tls_send_client_key_exchange_dhe ( struct tls_connection *tls ) {
                }
 
        err_send_handshake:
-       err_generate_keys:
        err_dhe_key:
                free ( dynamic );
        }
@@ -1608,9 +1593,23 @@ struct tls_key_exchange_algorithm tls_dhe_exchange_algorithm = {
 static int tls_send_client_key_exchange ( struct tls_connection *tls ) {
        struct tls_cipherspec *cipherspec = &tls->tx_cipherspec_pending;
        struct tls_cipher_suite *suite = cipherspec->suite;
+       int rc;
 
        /* Transmit Client Key Exchange record via key exchange algorithm */
-       return suite->exchange->exchange ( tls );
+       if ( ( rc = suite->exchange->exchange ( tls ) ) != 0 ) {
+               DBGC ( tls, "TLS %p could not exchange keys: %s\n",
+                      tls, strerror ( rc ) );
+               return rc;
+       }
+
+       /* Generate keys from master secret */
+       if ( ( rc = tls_generate_keys ( tls ) ) != 0 ) {
+               DBGC ( tls, "TLS %p could not generate keys: %s\n",
+                      tls, strerror ( rc ) );
+               return rc;
+       }
+
+       return 0;
 }
 
 /**