]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Fix SSL_pending() and SSL_has_pending() with DTLS
authorMatt Caswell <matt@openssl.org>
Mon, 25 Jul 2022 14:59:38 +0000 (15:59 +0100)
committerHugo Landau <hlandau@openssl.org>
Mon, 1 Aug 2022 07:07:45 +0000 (08:07 +0100)
If app data is received before a Finished message in DTLS then we buffer
it to return later. The function SSL_pending() is supposed to tell you
how much processed app data we have already buffered, and SSL_has_pending()
is supposed to tell you if we have any data buffered (whether processed or
not, and whether app data or not).

Neither SSL_pending() or SSL_has_pending() were taking account of this
DTLS specific app data buffer.

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

ssl/record/rec_layer_s3.c
ssl/ssl_lib.c

index 70bc3b763c73090367c03c9d5e523d19b8d567c6..f866792cdc8c339c337b32d992880c2748ff09d6 100644 (file)
@@ -119,10 +119,22 @@ size_t ssl3_pending(const SSL *s)
     if (sc->rlayer.rstate == SSL_ST_READ_BODY)
         return 0;
 
+    /* Take into account DTLS buffered app data */
+    if (SSL_CONNECTION_IS_DTLS(sc)) {
+        DTLS1_RECORD_DATA *rdata;
+        pitem *item, *iter;
+
+        iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q);
+        while ((item = pqueue_next(&iter)) != NULL) {
+            rdata = item->data;
+            num += rdata->rrec.length;
+        }
+    }
+
     for (i = 0; i < RECORD_LAYER_get_numrpipes(&sc->rlayer); i++) {
         if (SSL3_RECORD_get_type(&sc->rlayer.rrec[i])
             != SSL3_RT_APPLICATION_DATA)
-            return 0;
+            return num;
         num += SSL3_RECORD_get_length(&sc->rlayer.rrec[i]);
     }
 
index 24bcd7feecf703d17fd94276bd8b71565e0137d3..1a7242aee22134e07d1732a7c6aa2f1aab1f95b6 100644 (file)
@@ -1761,16 +1761,26 @@ int SSL_has_pending(const SSL *s)
 {
     /*
      * Similar to SSL_pending() but returns a 1 to indicate that we have
-     * unprocessed data available or 0 otherwise (as opposed to the number of
-     * bytes available). Unlike SSL_pending() this will take into account
-     * read_ahead data. A 1 return simply indicates that we have unprocessed
-     * data. That data may not result in any application data, or we may fail
-     * to parse the records for some reason.
+     * processed or unprocessed data available or 0 otherwise (as opposed to the
+     * number of bytes available). Unlike SSL_pending() this will take into
+     * account read_ahead data. A 1 return simply indicates that we have data.
+     * That data may not result in any application data, or we may fail to parse
+     * the records for some reason.
      */
     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
 
-    if (sc == NULL)
-        return 0;
+    /* Check buffered app data if any first */
+    if (SSL_CONNECTION_IS_DTLS(sc)) {
+        DTLS1_RECORD_DATA *rdata;
+        pitem *item, *iter;
+
+        iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q);
+        while ((item = pqueue_next(&iter)) != NULL) {
+            rdata = item->data;
+            if (rdata->rrec.length > 0)
+                return 1;
+        }
+    }
 
     if (RECORD_LAYER_processed_read_pending(&sc->rlayer))
         return 1;