Topic: TLS Handshake exchanging messages

I am doing  an handshake between a client and a server, after the handshake the client sends a message and waits for the server to reply with the same message. The first message always goes through, but the client is never able to read the second one and the ones after, it simply reads 0.

I found that the first loop ssl->buffers.clearOutputBuffer.length is equal to zero, but the second time it's like 900, so wolfSSL_read follows a different procedure, doesn't read and sets the buffer at zero. If in debug i set ssl->buffers.clearOutputBuffer.length to zero everything works. So i would like to know how can i do it in code, or what am i doing wrong.

    while (true)
    {
        printf("Send a string to the server\n"
               "x to exit\n");
        if (fgets(stringtosend, sizeof(stringtosend), stdin) == NULL) {
            printf("error reading");
        }
        do {
            ret = wolfSSL_write(ssl, stringtosend, sizeof(stringtosend));
            err = wolfSSL_get_error(ssl, ret);
        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
        printf("Sent (%d): %s\n", err, stringtosend);

        XMEMSET(readBuf, 0, sizeof(readBuf));
        do {
            ret = wolfSSL_read(ssl, readBuf, sizeof(readBuf)-1);
            err = wolfSSL_get_error(ssl, ret);
        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
        printf("Read (%d): %s\n", err, readBuf);
        
        
        //ssl->buffers.clearOutputBuffer.length = 0;
        if (stringtosend[0] == 'x' && stringtosend[1] == '\n'){
            return;
        }
    }

Share

Re: TLS Handshake exchanging messages

Hi astc

What is the server doing? If it is not sending messages, I could see where your test would break.

I modified our simple examples to do what you are trying to accomplish:
https://github.com/wolfSSL/wolfssl-exam … master/tls

diff --git a/tls/client-tls.c b/tls/client-tls.c
index d1e06be..9f13d84 100644
--- a/tls/client-tls.c
+++ b/tls/client-tls.c
@@ -133,32 +133,68 @@ int main(int argc, char** argv)
         goto cleanup;
     }
 
-    /* Get a message for the server from stdin */
-    printf("Message for server: ");
-    memset(buff, 0, sizeof(buff));
-    if (fgets(buff, sizeof(buff), stdin) == NULL) {
-        fprintf(stderr, "ERROR: failed to get message for server\n");
-        ret = -1;
-        goto cleanup;
-    }
-    len = strnlen(buff, sizeof(buff));
-
-    /* Send the message to the server */
-    if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
-        fprintf(stderr, "ERROR: failed to write entire message\n");
-        fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
-        goto cleanup;
+#if 1
+    while (1)
+    {
+        int err;
+        char stringtosend[1024];
+        char readBuf[1024];
+
+        printf("Send a string to the server\n"
+               "x to exit\n");
+        if (fgets(stringtosend, sizeof(stringtosend), stdin) == NULL) {
+            printf("error reading");
+        }
+        do {
+            ret = wolfSSL_write(ssl, stringtosend, sizeof(stringtosend));
+            err = wolfSSL_get_error(ssl, ret);
+        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
+        printf("Sent (%d): %s\n", err, stringtosend);
+
+        XMEMSET(readBuf, 0, sizeof(readBuf));
+        do {
+            ret = wolfSSL_read(ssl, readBuf, sizeof(readBuf)-1);
+            err = wolfSSL_get_error(ssl, ret);
+        } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
+        printf("Read (%d): %s\n", err, readBuf);
+
+
+        //ssl->buffers.clearOutputBuffer.length = 0;
+        if (stringtosend[0] == 'x' && stringtosend[1] == '\n'){
+            break;
+        }
     }
-
-    /* Read the server data into our buff array */
-    memset(buff, 0, sizeof(buff));
-    if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
-        fprintf(stderr, "ERROR: failed to read\n");
-        goto cleanup;
-    }
-
-    /* Print to stdout any data the server sends */
-    printf("Server: %s\n", buff);
+#else
+    do {
+        /* Get a message for the server from stdin */
+        printf("Message for server: ");
+        memset(buff, 0, sizeof(buff));
+        if (fgets(buff, sizeof(buff), stdin) == NULL) {
+            fprintf(stderr, "ERROR: failed to get message for server\n");
+            ret = -1;
+            goto cleanup;
+        }
+        len = strnlen(buff, sizeof(buff));
+
+        /* Send the message to the server */
+        if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
+            fprintf(stderr, "ERROR: failed to write entire message\n");
+            fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
+            goto cleanup;
+        }
+
+        /* Read the server data into our buff array */
+        memset(buff, 0, sizeof(buff));
+        if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
+            fprintf(stderr, "ERROR: failed to read\n");
+            goto cleanup;
+        }
+
+        /* Print to stdout any data the server sends */
+        printf("Server: %s\n", buff);
+
+    } while(1);
+#endif
 
     /* Bidirectional shutdown */
     while (wolfSSL_shutdown(ssl) == SSL_SHUTDOWN_NOT_DONE) {
diff --git a/tls/server-tls.c b/tls/server-tls.c
index fa79a4d..6fc3c50 100644
--- a/tls/server-tls.c
+++ b/tls/server-tls.c
@@ -160,35 +160,35 @@ int main()
 
         printf("Client connected successfully\n");
 
-
-
-        /* Read the client data into our buff array */
-        memset(buff, 0, sizeof(buff));
-        if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
-            fprintf(stderr, "ERROR: failed to read\n");
-            goto exit;
-        }
-
-        /* Print to stdout any data the client sends */
-        printf("Client: %s\n", buff);
-
-        /* Check for server shutdown command */
-        if (strncmp(buff, "shutdown", 8) == 0) {
-            printf("Shutdown command issued!\n");
-            shutdown = 1;
-        }
-
-
-
-        /* Write our reply into buff */
-        memset(buff, 0, sizeof(buff));
-        memcpy(buff, reply, strlen(reply));
-        len = strnlen(buff, sizeof(buff));
-
-        /* Reply back to the client */
-        if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
-            fprintf(stderr, "ERROR: failed to write\n");
-            goto exit;
+        while(!shutdown) {
+            /* Read the client data into our buff array */
+            memset(buff, 0, sizeof(buff));
+            if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
+                fprintf(stderr, "ERROR: failed to read\n");
+                goto exit;
+            }
+
+            /* Print to stdout any data the client sends */
+            printf("Client: %s\n", buff);
+
+            /* Check for server shutdown command */
+            if (strncmp(buff, "shutdown", 8) == 0) {
+                printf("Shutdown command issued!\n");
+                shutdown = 1;
+            }
+
+
+
+            /* Write our reply into buff */
+            memset(buff, 0, sizeof(buff));
+            memcpy(buff, reply, strlen(reply));
+            len = strnlen(buff, sizeof(buff));
+
+            /* Reply back to the client */
+            if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
+                fprintf(stderr, "ERROR: failed to write\n");
+                goto exit;
+            }
         }
 
         /* Notify the client that the connection is ending */

Re: TLS Handshake exchanging messages

Same behavior, but i have found an ugly fix

        /* Flush */
        memset(stringtosend, 0, sizeof(stringtosend));
        if ((ret = wolfSSL_read(ssl, stringtosend, sizeof(stringtosend)-1)) == -1) {
            fprintf(stderr, "ERROR: failed to read\n");
            goto done;
        }

        /* Read the server data into our buff array */
        if(stringtosend[0] == 0x0){
            if ((ret = wolfSSL_read(ssl, stringtosend, sizeof(stringtosend)-1)) == -1) {
                fprintf(stderr, "ERROR: failed to read\n");
                goto done;
            }
        }

Share