@@ -97,6 +97,7 @@ int main(int argc, char **argv)
9797 * where we think we're testing it.
9898 */
9999 const bool ktls_expected = (getenv ("S2N_KTLS_TESTING_EXPECTED" ) != NULL );
100+ const bool ktls_keyupdate_expected = (getenv ("S2N_KTLS_KEYUPDATE_TESTING_EXPECTED" ) != NULL );
100101
101102 if (!s2n_ktls_is_supported_on_platform () && !ktls_expected ) {
102103 END_TEST ();
@@ -460,22 +461,6 @@ int main(int argc, char **argv)
460461 EXPECT_BYTEARRAY_EQUAL (test_data , buffer , read );
461462 }
462463 };
463-
464- /* Test: receiving KeyUpdate message */
465- {
466- EXPECT_SUCCESS (s2n_connection_set_blinding (reader , S2N_SELF_SERVICE_BLINDING ));
467-
468- /* Write KeyUpdate message */
469- s2n_atomic_flag_set (& writer -> key_update_pending );
470- int written = s2n_send (writer , test_data , sizeof (test_data ), & blocked );
471- EXPECT_EQUAL (written , sizeof (test_data ));
472-
473- /* Read KeyUpdate message */
474- uint8_t buffer [sizeof (test_data )] = { 0 };
475- EXPECT_FAILURE_WITH_ERRNO (
476- s2n_recv (reader , buffer , sizeof (buffer ), & blocked ),
477- S2N_ERR_KTLS_KEYUPDATE );
478- };
479464 };
480465
481466 /* Test: s2n_shutdown
@@ -698,5 +683,124 @@ int main(int argc, char **argv)
698683 }
699684 }
700685
686+ /* Test: Keyupdates with KTLS */
687+ if (ktls_keyupdate_expected ) {
688+ /* Cipher suite with an artificially lowered encryption limit */
689+ const size_t test_encryption_limit = 1 ;
690+ struct s2n_record_algorithm test_record_alg = * s2n_tls13_aes_128_gcm_sha256 .record_alg ;
691+ test_record_alg .encryption_limit = test_encryption_limit ;
692+ struct s2n_cipher_suite test_cipher_suite = s2n_tls13_aes_128_gcm_sha256 ;
693+ test_cipher_suite .record_alg = & test_record_alg ;
694+
695+ /* Test: Sending key update with KTLS */
696+ if (ktls_send_supported ) {
697+ /* Test: Multiple key updates are allowed as long as they can be sent over multiple
698+ * s2n_send calls with ktls. */
699+ {
700+ DEFER_CLEANUP (struct s2n_connection * client = s2n_connection_new (S2N_CLIENT ),
701+ s2n_connection_ptr_free );
702+ EXPECT_NOT_NULL (client );
703+ EXPECT_SUCCESS (s2n_connection_set_config (client , config ));
704+ EXPECT_SUCCESS (s2n_connection_set_cipher_preferences (client , "default_tls13" ));
705+ EXPECT_SUCCESS (s2n_connection_set_blinding (client , S2N_SELF_SERVICE_BLINDING ));
706+
707+ DEFER_CLEANUP (struct s2n_connection * server = s2n_connection_new (S2N_SERVER ),
708+ s2n_connection_ptr_free );
709+ EXPECT_NOT_NULL (server );
710+ EXPECT_SUCCESS (s2n_connection_set_config (server , config ));
711+ EXPECT_SUCCESS (s2n_connection_set_cipher_preferences (server , "default_tls13" ));
712+ EXPECT_SUCCESS (s2n_connection_set_blinding (server , S2N_SELF_SERVICE_BLINDING ));
713+
714+ DEFER_CLEANUP (struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close );
715+ EXPECT_OK (s2n_new_inet_socket_pair (& io_pair ));
716+ EXPECT_OK (s2n_setup_connections (server , client , & io_pair ));
717+
718+ EXPECT_SUCCESS (s2n_connection_ktls_enable_send (server ));
719+
720+ /* Reset server cipher suite to trigger a key update after sending one record */
721+ EXPECT_NOT_NULL (server -> secure );
722+ EXPECT_EQUAL (server -> secure -> cipher_suite , & s2n_tls13_aes_128_gcm_sha256 );
723+ server -> secure -> cipher_suite = & test_cipher_suite ;
724+
725+ /* This will require a keyupdate mid-send, which is not allowed with ktls */
726+ uint8_t exceeds_record_limit [S2N_TLS_MAXIMUM_FRAGMENT_LENGTH * 2 ] = { 0 };
727+ s2n_blocked_status blocked = S2N_NOT_BLOCKED ;
728+ EXPECT_FAILURE_WITH_ERRNO (s2n_send (server , exceeds_record_limit , sizeof (exceeds_record_limit ),
729+ & blocked ),
730+ S2N_ERR_INVALID_ARGUMENT );
731+
732+ uint8_t large_test_data [S2N_TLS_MAXIMUM_FRAGMENT_LENGTH ] = { "Hello there" };
733+ /* Each send call will include one key update */
734+ for (int i = 0 ; i < 10 ; i ++ ) {
735+ int written = s2n_send (server , large_test_data , sizeof (large_test_data ), & blocked );
736+ EXPECT_EQUAL (written , sizeof (large_test_data ));
737+ EXPECT_EQUAL (blocked , S2N_NOT_BLOCKED );
738+ }
739+ EXPECT_EQUAL (server -> recv_key_updated , 0 );
740+ EXPECT_EQUAL (server -> send_key_updated , 9 );
741+
742+ /* We only get one record per s2n_recv call, so we call it ten times */
743+ for (size_t i = 0 ; i < 10 ; i ++ ) {
744+ uint8_t buffer [sizeof (large_test_data )] = { 1 };
745+ int read = s2n_recv (client , buffer , sizeof (buffer ), & blocked );
746+ EXPECT_EQUAL (read , sizeof (large_test_data ));
747+ EXPECT_BYTEARRAY_EQUAL (large_test_data , buffer , read );
748+ }
749+
750+ EXPECT_EQUAL (client -> recv_key_updated , 9 );
751+ EXPECT_EQUAL (client -> send_key_updated , 0 );
752+ }
753+ };
754+
755+ /* Test: Receiving key update with KTLS */
756+ if (ktls_recv_supported ) {
757+ DEFER_CLEANUP (struct s2n_connection * client = s2n_connection_new (S2N_CLIENT ),
758+ s2n_connection_ptr_free );
759+ EXPECT_NOT_NULL (client );
760+ EXPECT_SUCCESS (s2n_connection_set_config (client , config ));
761+ EXPECT_SUCCESS (s2n_connection_set_cipher_preferences (client , "default_tls13" ));
762+ EXPECT_SUCCESS (s2n_connection_set_blinding (client , S2N_SELF_SERVICE_BLINDING ));
763+
764+ DEFER_CLEANUP (struct s2n_connection * server = s2n_connection_new (S2N_SERVER ),
765+ s2n_connection_ptr_free );
766+ EXPECT_NOT_NULL (server );
767+ EXPECT_SUCCESS (s2n_connection_set_config (server , config ));
768+ EXPECT_SUCCESS (s2n_connection_set_cipher_preferences (server , "default_tls13" ));
769+ EXPECT_SUCCESS (s2n_connection_set_blinding (server , S2N_SELF_SERVICE_BLINDING ));
770+
771+ DEFER_CLEANUP (struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close );
772+ EXPECT_OK (s2n_new_inet_socket_pair (& io_pair ));
773+ EXPECT_OK (s2n_setup_connections (server , client , & io_pair ));
774+
775+ EXPECT_SUCCESS (s2n_connection_ktls_enable_recv (client ));
776+
777+ /* Reset server cipher suite to trigger a key update after sending one record */
778+ EXPECT_NOT_NULL (server -> secure );
779+ EXPECT_EQUAL (server -> secure -> cipher_suite , & s2n_tls13_aes_128_gcm_sha256 );
780+ server -> secure -> cipher_suite = & test_cipher_suite ;
781+
782+ uint8_t large_test_data [S2N_DEFAULT_FRAGMENT_LENGTH * 10 ] = { "Hello there" };
783+ s2n_blocked_status blocked = S2N_NOT_BLOCKED ;
784+ int written = s2n_send (server , large_test_data , sizeof (large_test_data ), & blocked );
785+ EXPECT_EQUAL (written , sizeof (large_test_data ));
786+ EXPECT_EQUAL (blocked , S2N_NOT_BLOCKED );
787+
788+ /* Sent 10 records and the encryption limit is 1 record so the send key will be updated
789+ * 9 times. */
790+ EXPECT_EQUAL (server -> recv_key_updated , 0 );
791+ EXPECT_EQUAL (server -> send_key_updated , 9 );
792+
793+ /* We only get one record per s2n_recv call, so we call it ten times */
794+ for (size_t i = 0 ; i < 10 ; i ++ ) {
795+ uint8_t buffer [sizeof (large_test_data ) / 10 ] = { 1 };
796+ int read = s2n_recv (client , buffer , sizeof (buffer ), & blocked );
797+ EXPECT_EQUAL (read , sizeof (large_test_data ) / 10 );
798+ EXPECT_BYTEARRAY_EQUAL (buffer , large_test_data + (i * S2N_DEFAULT_FRAGMENT_LENGTH ), read );
799+ }
800+
801+ EXPECT_EQUAL (client -> recv_key_updated , 9 );
802+ EXPECT_EQUAL (client -> send_key_updated , 0 );
803+ };
804+ }
701805 END_TEST ();
702806}
0 commit comments