]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Introduce a step to prepare the BIO before writing
authorMatt Caswell <matt@openssl.org>
Tue, 4 Oct 2022 15:32:02 +0000 (16:32 +0100)
committerMatt Caswell <matt@openssl.org>
Wed, 12 Oct 2022 14:53:31 +0000 (15:53 +0100)
This removes some KTLS specific code from tls_retry_write_records().

Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19343)

ssl/record/methods/ktls_meth.c
ssl/record/methods/recmethod_local.h
ssl/record/methods/ssl3_meth.c
ssl/record/methods/tls13_meth.c
ssl/record/methods/tls1_meth.c
ssl/record/methods/tls_common.c
ssl/record/methods/tlsany_meth.c

index 6715e1c65d7f5ae7a539afe360f1fc8c2197d5d2..14db12ad5b6620161fc5145f12e8f159caacd7cd 100644 (file)
@@ -511,6 +511,28 @@ static int ktls_post_encryption_processing(OSSL_RECORD_LAYER *rl,
     return 1;
 }
 
+static int ktls_prepare_write_bio(OSSL_RECORD_LAYER *rl, int type)
+{
+    /*
+     * To prevent coalescing of control and data messages,
+     * such as in buffer_write, we flush the BIO
+     */
+    if (type != SSL3_RT_APPLICATION_DATA) {
+        int ret, i = BIO_flush(rl->bio);
+
+        if (i <= 0) {
+            if (BIO_should_retry(rl->bio))
+                ret = OSSL_RECORD_RETURN_RETRY;
+            else
+                ret = OSSL_RECORD_RETURN_FATAL;
+            return ret;
+        }
+        BIO_set_ktls_ctrl_msg(rl->bio, type);
+    }
+
+    return OSSL_RECORD_RETURN_SUCCESS;
+}
+
 static struct record_functions_st ossl_ktls_funcs = {
     ktls_set_crypto_state,
     ktls_cipher,
@@ -528,7 +550,8 @@ static struct record_functions_st ossl_ktls_funcs = {
     ktls_prepare_record_header,
     NULL,
     ktls_prepare_for_encryption,
-    ktls_post_encryption_processing
+    ktls_post_encryption_processing,
+    ktls_prepare_write_bio
 };
 
 const OSSL_RECORD_METHOD ossl_ktls_record_method = {
index 7f4ede9f4d024aa22e5b426d8a1be82c86ca3cb7..cb689163647b8285f58dd4120db4c21d1583ddd6 100644 (file)
@@ -130,6 +130,13 @@ struct record_functions_st
                                       OSSL_RECORD_TEMPLATE *thistempl,
                                       WPACKET *thispkt,
                                       SSL3_RECORD *thiswr);
+
+    /*
+     * Some record layer implementations need to do some custom preparation of
+     * the BIO before we write to it. KTLS does this to prevent coalescing of
+     * control and data messages.
+     */
+    int (*prepare_write_bio)(OSSL_RECORD_LAYER *rl, int type);
 };
 
 struct ossl_record_layer_st
index 9b2d63e9b8b308049aa61e24bf9992e0e9f3ae8f..90cf5542c3c1f5e6ffa9027e3c027a2f9d8f3d49 100644 (file)
@@ -318,5 +318,6 @@ struct record_functions_st ssl_3_0_funcs = {
     tls_prepare_record_header_default,
     NULL,
     tls_prepare_for_encryption_default,
-    tls_post_encryption_processing_default
+    tls_post_encryption_processing_default,
+    NULL
 };
index 8a3bdb254feae201e869a66f8b1a29a04b5798a9..ad22f11bf1d42d820c1ec177142cd5f8ba8f1d56 100644 (file)
@@ -326,5 +326,6 @@ struct record_functions_st tls_1_3_funcs = {
     tls_prepare_record_header_default,
     tls13_add_record_padding,
     tls_prepare_for_encryption_default,
-    tls_post_encryption_processing_default
+    tls_post_encryption_processing_default,
+    NULL
 };
index bd3c32832bc9ff0ed1d7c38711d77c8441ea36bc..43907957470169832d47b4e599fcc3f2d557f57c 100644 (file)
@@ -659,7 +659,8 @@ struct record_functions_st tls_1_funcs = {
     tls_prepare_record_header_default,
     NULL,
     tls_prepare_for_encryption_default,
-    tls_post_encryption_processing_default
+    tls_post_encryption_processing_default,
+    NULL
 };
 
 struct record_functions_st dtls_1_funcs = {
@@ -678,5 +679,6 @@ struct record_functions_st dtls_1_funcs = {
     NULL,
     NULL,
     NULL,
+    NULL,
     NULL
 };
index 66dcbe2ab4265d642fccf875d6e38e3220140676..d3f643918480a8d92805f5f7224a7f6d88230f43 100644 (file)
@@ -1851,21 +1851,10 @@ int tls_retry_write_records(OSSL_RECORD_LAYER *rl)
 
         clear_sys_error();
         if (rl->bio != NULL) {
-            /*
-             * To prevent coalescing of control and data messages,
-             * such as in buffer_write, we flush the BIO
-             */
-            if (BIO_get_ktls_send(rl->bio)
-                    && thiswb->type != SSL3_RT_APPLICATION_DATA) {
-                i = BIO_flush(rl->bio);
-                if (i <= 0) {
-                    if (BIO_should_retry(rl->bio))
-                        ret = OSSL_RECORD_RETURN_RETRY;
-                    else
-                        ret = OSSL_RECORD_RETURN_FATAL;
+            if (rl->funcs->prepare_write_bio != NULL) {
+                ret = rl->funcs->prepare_write_bio(rl, thiswb->type);
+                if (ret != OSSL_RECORD_RETURN_SUCCESS)
                     return ret;
-                }
-                BIO_set_ktls_ctrl_msg(rl->bio, thiswb->type);
             }
             i = BIO_write(rl->bio, (char *)
                           &(SSL3_BUFFER_get_buf(thiswb)
index b18c475ed2d571e2eb31578fa92795aa758dcab3..09d2c2926a03c3f908ea2bfd32a38fb772afd1c4 100644 (file)
@@ -160,7 +160,8 @@ struct record_functions_st tls_any_funcs = {
     tls_prepare_record_header_default,
     NULL,
     tls_any_prepare_for_encryption,
-    tls_post_encryption_processing_default
+    tls_post_encryption_processing_default,
+    NULL
 };
 
 static int dtls_any_set_protocol_version(OSSL_RECORD_LAYER *rl, int vers)
@@ -189,5 +190,6 @@ struct record_functions_st dtls_any_funcs = {
     NULL,
     NULL,
     NULL,
+    NULL,
     NULL
 };