Skip to contentSkip to conversation

Introduce macros for the returns values of key_state_*

PATCH In review Frank Lichtenheld 1 participant 2 messages May 5, 2022 – Jun 28, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Introduce macros for the returns values of key_state_*

I think that makes the code slightly more readable.

---
 src/openvpn/ssl.c         | 20 +++++++++---------
 src/openvpn/ssl_backend.h | 35 ++++++++++++++++++-------------
 src/openvpn/ssl_mbedtls.c | 44 +++++++++++++++++++--------------------
 3 files changed, 52 insertions(+), 47 deletions(-)

Might conflict with some of Arne's handshake patches.

src/openvpn/ssl.c +10 -10
index 5b0cdcaa..b174b723 100644
@@ -2599,7 +2599,7 @@ tls_process_state(struct tls_multi *multi,
         if (buf->len)
         {
             status = key_state_write_ciphertext(&ks->ks_ssl, buf);
-            if (status == -1)
+            if (status == KS_IO_ERROR)
             {
                 msg(D_TLS_ERRORS,
                     "TLS Error: Incoming Ciphertext -> TLS object write error");
@@ -2608,9 +2608,9 @@ tls_process_state(struct tls_multi *multi,
         }
         else
         {
-            status = 1;
+            status = KS_IO_SUCCESS;
         }
-        if (status == 1)
+        if (status == KS_IO_SUCCESS)
         {
             reliable_mark_deleted(ks->rec_reliable, buf);
             state_change = true;
@@ -2627,12 +2627,12 @@ tls_process_state(struct tls_multi *multi,
         ASSERT(buf_init(buf, 0));
         status = key_state_read_plaintext(&ks->ks_ssl, buf, TLS_CHANNEL_BUF_SIZE);
         update_time();
-        if (status == -1)
+        if (status == KS_IO_ERROR)
         {
             msg(D_TLS_ERRORS, "TLS Error: TLS object -> incoming plaintext read error");
             goto error;
         }
-        if (status == 1)
+        if (status == KS_IO_SUCCESS)
         {
             state_change = true;
             dmsg(D_TLS_DEBUG, "TLS -> Incoming Plaintext");
@@ -2678,13 +2678,13 @@ tls_process_state(struct tls_multi *multi,
     if (buf->len)
     {
         int status = key_state_write_plaintext(&ks->ks_ssl, buf);
-        if (status == -1)
+        if (status == KS_IO_ERROR)
         {
             msg(D_TLS_ERRORS,
                 "TLS ERROR: Outgoing Plaintext -> TLS object write error");
             goto error;
         }
-        if (status == 1)
+        if (status == KS_IO_SUCCESS)
         {
             state_change = true;
             dmsg(D_TLS_DEBUG, "Outgoing Plaintext -> TLS");
@@ -2699,13 +2699,13 @@ tls_process_state(struct tls_multi *multi,
         {
             int status = key_state_read_ciphertext(&ks->ks_ssl, buf, multi->opt.frame.tun_mtu);
 
-            if (status == -1)
+            if (status == KS_IO_ERROR)
             {
                 msg(D_TLS_ERRORS,
                     "TLS Error: Ciphertext -> reliable TCP/UDP transport read error");
                 goto error;
             }
-            if (status == 1)
+            if (status == KS_IO_SUCCESS)
             {
                 reliable_mark_active_outgoing(ks->send_reliable, buf, P_CONTROL_V1);
                 INCR_GENERATED;
@@ -3689,7 +3689,7 @@ tls_send_payload(struct tls_multi *multi,
 
     if (ks->state >= S_ACTIVE)
     {
-        if (key_state_write_plaintext_const(&ks->ks_ssl, data, size) == 1)
+        if (key_state_write_plaintext_const(&ks->ks_ssl, data, size) == KS_IO_SUCCESS)
         {
             ret = true;
         }
src/openvpn/ssl_backend.h +20 -15
index 1bd33699..d68f02aa 100644
@@ -32,6 +32,11 @@
 
 #include "buffer.h"
 
+/* key_state_{read,write}_* return values */
+#define KS_IO_SUCCESS 1
+#define KS_IO_RETRY   0
+#define KS_IO_ERROR  -1
+
 #ifdef ENABLE_CRYPTO_OPENSSL
 #include "ssl_openssl.h"
 #include "ssl_verify_openssl.h"
@@ -427,10 +432,10 @@ key_state_export_keying_material(struct tls_session *session,
  *
  * @return The return value indicates whether the data was successfully
  *     processed:
- * - \c 1: All the data was processed successfully.
- * - \c 0: The data was not processed, this function should be called
+ * - \c KS_IO_SUCCESS: All the data was processed successfully.
+ * - \c KS_IO_RETRY: The data was not processed, this function should be called
  *   again later to retry.
- * - \c -1: An error occurred.
+ * - \c KS_IO_ERROR: An error occurred.
  */
 int key_state_write_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf);
 
@@ -444,10 +449,10 @@ int key_state_write_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf);
  *
  * @return The return value indicates whether the data was successfully
  *     processed:
- * - \c 1: All the data was processed successfully.
- * - \c 0: The data was not processed, this function should be called
+ * - \c KS_IO_SUCCESS: All the data was processed successfully.
+ * - \c KS_IO_RETRY: The data was not processed, this function should be called
  *   again later to retry.
- * - \c -1: An error occurred.
+ * - \c KS_IO_ERROR: An error occurred.
  */
 int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,
                                     const uint8_t *data, int len);
@@ -465,10 +470,10 @@ int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl,
  *
  * @return The return value indicates whether the data was successfully
  *     processed:
- * - \c 1: Data was extracted successfully.
- * - \c 0: No data was extracted, this function should be called again
+ * - \c KS_IO_SUCCESS: Data was extracted successfully.
+ * - \c KS_IO_RETRY: No data was extracted, this function should be called again
  *   later to retry.
- * - \c -1: An error occurred.
+ * - \c KS_IO_ERROR: An error occurred.
  */
 int key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf,
                               int maxlen);
@@ -491,10 +496,10 @@ int key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf,
  *
  * @return The return value indicates whether the data was successfully
  *     processed:
- * - \c 1: All the data was processed successfully.
- * - \c 0: The data was not processed, this function should be called
+ * - \c KS_IO_SUCCESS: All the data was processed successfully.
+ * - \c KS_IO_RETRY: The data was not processed, this function should be called
  *   again later to retry.
- * - \c -1: An error occurred.
+ * - \c KS_IO_ERROR: An error occurred.
  */
 int key_state_write_ciphertext(struct key_state_ssl *ks_ssl,
                                struct buffer *buf);
@@ -512,10 +517,10 @@ int key_state_write_ciphertext(struct key_state_ssl *ks_ssl,
  *
  * @return The return value indicates whether the data was successfully
  *     processed:
- * - \c 1: Data was extracted successfully.
- * - \c 0: No data was extracted, this function should be called again
+ * - \c KS_IO_SUCCESS: Data was extracted successfully.
+ * - \c KS_IO_RETRY: No data was extracted, this function should be called again
  *   later to retry.
- * - \c -1: An error occurred.
+ * - \c KS_IO_ERROR: An error occurred.
  */
 int key_state_read_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf,
                              int maxlen);
src/openvpn/ssl_mbedtls.c +22 -22

164 lines not on this page · raw message

Signature · 1 line
2.30.2
#1 lore raw

Note that this patch is wrong. Do not waste time reviewing it. Maybe at some point I will send a v2. But since it is not high priority it might take some time.

Quoted 11 lines · Frank Lichtenheld

I think that makes the code slightly more readable.

---
 src/openvpn/ssl.c         | 20 +++++++++---------
 src/openvpn/ssl_backend.h | 35 ++++++++++++++++++-------------
 src/openvpn/ssl_mbedtls.c | 44 +++++++++++++++++++--------------------
 3 files changed, 52 insertions(+), 47 deletions(-)

Might conflict with some of Arne's handshake patches.

[...]

Signature · 1 line
  Frank Lichtenheld