rust: bssl-tls: Fix flush protocol

There are two problems with the current BIO flush implementation.
- We did not signal retry correctly during flushing
- We did not test the retry reason correctly per specification

Signed-off-by: Xiangfei Ding <xfding@google.com>
Change-Id: Iff3a922dec08579740edfe9882fd74a36a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/101407
Reviewed-by: Rudolf Polzer <rpolzer@google.com>
diff --git a/rust/bssl-tls/src/connection/io.rs b/rust/bssl-tls/src/connection/io.rs
index 16fa610..a25d08b 100644
--- a/rust/bssl-tls/src/connection/io.rs
+++ b/rust/bssl-tls/src/connection/io.rs
@@ -26,10 +26,11 @@
 };
 
 use crate::{
+    ReceiveBuffer,
     connection::{
+        TlsConnection,
         lifecycle::ShutdownStatus,
         methods::HasTlsConnectionMethod, //
-        TlsConnection,
     },
     context::{
         HasDatagramIo,
@@ -43,7 +44,6 @@
     },
     ffi::slice_into_ffi_raw_parts,
     io::IoStatus, //
-    ReceiveBuffer,
 };
 
 impl<R, M> TlsConnection<R, M>
@@ -227,7 +227,7 @@
             // Safety: `bio` should still be valid here.
             bssl_sys::BIO_should_retry(bio)
         };
-        if bio_retry != 1 {
+        if bio_retry != 0 {
             return Ok(IoStatus::Retry(TlsRetryReason::WantWrite));
         }
         // Pre-emptively extract error and clear the error queue.
diff --git a/rust/bssl-tls/src/io.rs b/rust/bssl-tls/src/io.rs
index 240765f..ca5a9ea 100644
--- a/rust/bssl-tls/src/io.rs
+++ b/rust/bssl-tls/src/io.rs
@@ -484,7 +484,14 @@
     };
     let res = abort_on_panic(work);
     match rust_bio.transform_result(res, TlsRetryReason::WantWrite) {
-        IoStatus::Ok(_) | IoStatus::Retry(_) => 1,
+        IoStatus::Ok(_) => 1,
+        IoStatus::Retry(_) => {
+            unsafe {
+                // Safety: `bio` is still valid now.
+                bssl_sys::BIO_set_retry_write(bio);
+            }
+            0
+        }
         IoStatus::EndOfStream => {
             rust_bio.write_eos = true;
             0