Commit 5be87c4438 for openssl.org

commit 5be87c4438d91d7f6f28265b1d79ad9565731da3
Author: Refia <112306763+refiaa@users.noreply.github.com>
Date:   Thu Nov 12 19:08:23 2026 +0900

    Preserve HTTP response state across retries

    OSSL_HTTP_REQ_CTX_nbio() kept the response status code, the number of
    response header lines, whether the expected Content-Type and keep-alive
    had been seen, and the error response content length in local
    variables. When the function returned -1 on BIO_should_retry() and was
    called again, these values were reset. Depending only on how the
    transport split the response, the response header line limit could be
    exceeded, the status-dependent Content-Type check could be skipped,
    and valid responses could fail with HTTP_R_MISSING_CONTENT_TYPE or
    HTTP_R_SERVER_CANCELED_CONNECTION.

    Keep this state in OSSL_HTTP_REQ_CTX and reset it when reading of the
    response starts. Add a test that feeds the same responses at once and
    one byte at a time and expects the same result.

    Fixes #33679

    Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
    Reviewed-by: Mounir Idrassi <mounir.idrassi@idrix.fr>
    Merge-date: Wed Nov 18 12:13:00 2026
    Merged-from: https://github.com/openssl/openssl/pull/33681

diff --git a/crypto/http/http_client.c b/crypto/http/http_client.c
index 6cca340923..5ca2250aae 100644
--- a/crypto/http/http_client.c
+++ b/crypto/http/http_client.c
@@ -71,6 +71,17 @@ struct ossl_http_req_ctx_st {
     time_t max_total_time; /* Maximum end time of total transfer, or 0 */
     char *redirection_url; /* Location obtained from HTTP status 301/302 */
     size_t max_hdr_lines; /* Max. number of response header lines, or 0 */
+    /*
+     * Parsing state of the response being received, reset when reading of
+     * the response starts and kept across OSSL_HTTP_REQ_CTX_nbio() calls
+     * that return -1
+     */
+    int status_code; /* HTTP status code of response being received */
+    int got_text; /* Response Content-Type is (likely) text */
+    int found_expected_ct; /* Expected Content-Type was found in response */
+    int found_keep_alive; /* Response indicates persistent connection */
+    size_t resp_hdr_lines; /* Number of response header lines received */
+    size_t err_content_len; /* Length of error response content received */
 };

 /* HTTP client OSSL_HTTP_REQ_CTX_nbio() internal states, in typical order */
@@ -568,14 +579,11 @@ static int may_still_retry(time_t max_time, int *ptimeout)
  */
 int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
 {
-    int i, found_expected_ct = 0, found_keep_alive = 0;
-    int status_code = 0;
-    int got_text = 1;
+    int i;
     long n;
-    size_t resp_len = 0;
+    size_t resp_len;
     const unsigned char *p;
     char *buf, *key, *value, *line_end = NULL;
-    size_t resp_hdr_lines = 0;

     if (rctx == NULL) {
         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
@@ -607,7 +615,8 @@ next_io:
                 if (OSSL_TRACE_ENABLED(HTTP))
                     OSSL_TRACE(HTTP, "]\n"); /* end of error response content */
                 /* in addition, throw error on inconsistent length: */
-                (void)check_set_resp_len("error response content", rctx, resp_len);
+                (void)check_set_resp_len("error response content", rctx,
+                    rctx->err_content_len);
                 return 0;
             }
             if (BIO_should_retry(rctx->rbio))
@@ -696,6 +705,13 @@ next_io:
         i = BIO_flush(rctx->wbio);

         if (i > 0) {
+            /* start of response: reset response-wide parsing state */
+            rctx->status_code = 0;
+            rctx->got_text = 1;
+            rctx->found_expected_ct = 0;
+            rctx->found_keep_alive = 0;
+            rctx->resp_hdr_lines = 0;
+            rctx->err_content_len = 0;
             rctx->state = OHS_FIRSTLINE;
             goto next_io;
         }
@@ -739,16 +755,18 @@ next_io:
         }

         if (rctx->state == OHS_ERROR_CONTENT) {
-            resp_len += n;
-            if (!check_max_len("error response content", rctx->max_resp_len, resp_len))
+            rctx->err_content_len += n;
+            if (!check_max_len("error response content", rctx->max_resp_len,
+                    rctx->err_content_len))
                 return 0;
             if (OSSL_TRACE_ENABLED(HTTP)) /* dump response content line */
-                OSSL_TRACE_STRING(HTTP, got_text, 1, (unsigned char *)buf, n);
+                OSSL_TRACE_STRING(HTTP, rctx->got_text, 1, (unsigned char *)buf, n);
             goto next_line;
         }

-        resp_hdr_lines++;
-        if (rctx->max_hdr_lines != 0 && rctx->max_hdr_lines < resp_hdr_lines) {
+        rctx->resp_hdr_lines++;
+        if (rctx->max_hdr_lines != 0
+            && rctx->max_hdr_lines < rctx->resp_hdr_lines) {
             ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_TOO_MANY_HDRLINES);
             rctx->state = OHS_ERROR;
             return 0;
@@ -770,8 +788,8 @@ next_io:

         /* First line in response header */
         if (rctx->state == OHS_FIRSTLINE) {
-            status_code = parse_http_line1(buf, &found_keep_alive);
-            switch (status_code) {
+            rctx->status_code = parse_http_line1(buf, &rctx->found_keep_alive);
+            switch (rctx->status_code) {
             case HTTP_STATUS_CODE_OK:
                 rctx->state = OHS_HEADERS;
                 goto next_line;
@@ -786,7 +804,7 @@ next_io:
                 /* fall through */
             default:
                 /* must return content if status >= 400 */
-                rctx->state = status_code < HTTP_STATUS_CODES_NONFATAL_ERROR
+                rctx->state = rctx->status_code < HTTP_STATUS_CODES_NONFATAL_ERROR
                     ? OHS_HEADERS_ERROR
                     : OHS_HEADERS;
                 goto next_line; /* continue parsing, also on HTTP error */
@@ -815,15 +833,15 @@ next_io:
                 return 0;
             }
             if (OPENSSL_strcasecmp(key, "Content-Type") == 0) {
-                got_text = HAS_CASE_PREFIX(value, "text/");
-                if (got_text
+                rctx->got_text = HAS_CASE_PREFIX(value, "text/");
+                if (rctx->got_text
                     && rctx->state == OHS_HEADERS
                     && rctx->expect_asn1
-                    && (status_code >= HTTP_STATUS_CODES_NONFATAL_ERROR
-                        || status_code == HTTP_STATUS_CODE_OK)) {
+                    && (rctx->status_code >= HTTP_STATUS_CODES_NONFATAL_ERROR
+                        || rctx->status_code == HTTP_STATUS_CODE_OK)) {
                     ERR_raise_data(ERR_LIB_HTTP, HTTP_R_CONTENT_TYPE_MISMATCH,
                         "expected ASN.1 content but got http code %d with Content-Type: %s",
-                        status_code, value);
+                        rctx->status_code, value);
                     rctx->state = OHS_HEADERS_ERROR;
                     goto next_line;
                 }
@@ -845,16 +863,16 @@ next_io:
                             rctx->expected_ct, value);
                         return 0;
                     }
-                    found_expected_ct = 1;
+                    rctx->found_expected_ct = 1;
                 }
             }

             /* https://tools.ietf.org/html/rfc7230#section-6.3 Persistence */
             if (OPENSSL_strcasecmp(key, "Connection") == 0) {
                 if (OPENSSL_strcasecmp(value, "keep-alive") == 0)
-                    found_keep_alive = 1;
+                    rctx->found_keep_alive = 1;
                 else if (OPENSSL_strcasecmp(value, "close") == 0)
-                    found_keep_alive = 0;
+                    rctx->found_keep_alive = 0;
             } else if (OPENSSL_strcasecmp(key, "Content-Length") == 0) {
                 size_t content_len = (size_t)strtoul(value, &line_end, 10);

@@ -882,7 +900,7 @@ next_io:
             OSSL_TRACE(HTTP, "]\n"); /* end of response header */

         if (rctx->keep_alive != 0 /* do not let server initiate keep_alive */
-            && !found_keep_alive /* otherwise there is no change */) {
+            && !rctx->found_keep_alive /* otherwise there is no change */) {
             if (rctx->keep_alive == 2) {
                 rctx->keep_alive = 0;
                 ERR_raise(ERR_LIB_HTTP, HTTP_R_SERVER_CANCELED_CONNECTION);
@@ -895,7 +913,7 @@ next_io:
             rctx->state = OHS_ERROR_CONTENT;
             if (OSSL_TRACE_ENABLED(HTTP)) {
                 OSSL_TRACE1(HTTP, "Receiving error response content (likely %s): [\n",
-                    got_text ? "text" : "ASN.1");
+                    rctx->got_text ? "text" : "ASN.1");
                 goto next_line;
             }
             /* discard response content when trace not enabled */
@@ -903,7 +921,7 @@ next_io:
             return 0;
         }

-        if (rctx->expected_ct != NULL && !found_expected_ct) {
+        if (rctx->expected_ct != NULL && !rctx->found_expected_ct) {
             ERR_raise_data(ERR_LIB_HTTP, HTTP_R_MISSING_CONTENT_TYPE,
                 "expected=%s", rctx->expected_ct);
             return 0;
diff --git a/test/http_test.c b/test/http_test.c
index 70921f5d8b..8877bbf1f3 100644
--- a/test/http_test.c
+++ b/test/http_test.c
@@ -13,6 +13,7 @@
 #include <openssl/pem.h>
 #include <openssl/x509v3.h>
 #include <openssl/err.h>
+#include <limits.h>
 #include <string.h>

 #include "testutil.h"
@@ -720,6 +721,299 @@ static int test_hdr_resp_hdr_limit_256(void)
     return test_http_resp_hdr_limit(256);
 }

+/**
+ * @struct resp_test_fixture_st
+ * @brief An HTTP response to be received via OSSL_HTTP_REQ_CTX_nbio(),
+ * with the request settings and the outcome expected for it.
+ */
+typedef struct resp_test_fixture_st {
+    const char *resp; /**< Response bytes */
+    size_t resp_len; /**< Length of resp */
+    size_t max_hdr_lines; /**< Max. number of response header lines, or 0 */
+    const char *expected_ct; /**< Expected Content-Type, or NULL */
+    int expect_asn1; /**< Response content must be ASN.1-encoded */
+    int keep_alive; /**< Persistent conn. 0=no, 1=prefer, 2=require */
+    int exp_ret; /**< Expected final result of OSSL_HTTP_REQ_CTX_nbio() */
+    int exp_reason; /**< Expected HTTP error reason code if exp_ret == 0 */
+    size_t expected_body_len; /**< Expected ASN.1 body length on success */
+    int expect_alive; /**< Expected connection persistence on success */
+} RESP_TEST_FIXTURE;
+
+/**
+ * @def RESP(s)
+ * Expands to the resp and resp_len initialisers of RESP_TEST_FIXTURE.
+ * @param s string literal holding the response
+ */
+#define RESP(s) s, sizeof(s) - 1
+
+/**
+ * @def KEEP_ALIVE_HDR
+ * Request header including the preceding and terminating CRLF.
+ */
+#define KEEP_ALIVE_HDR "\r\nConnection: keep-alive\r\n"
+
+/**
+ * @brief Send a GET request on |rctx| and receive |fixture->resp|.
+ * Checks that the request carries "Connection: keep-alive" exactly when
+ * keep-alive is requested, and that the outcome is the expected one.
+ * @param rctx context created by new_nbio_ctx()
+ * @param wbio request BIO of |rctx|
+ * @param rbio response BIO chain of |rctx|
+ * @param fixture response to receive and expected outcome
+ * @param piecemeal if nonzero, provide the response one byte per call
+ * of OSSL_HTTP_REQ_CTX_nbio(), else all at once
+ * @returns 1 if all checks pass, else 0
+ */
+static int exchange(OSSL_HTTP_REQ_CTX *rctx, BIO *wbio, BIO *rbio,
+    const RESP_TEST_FIXTURE *fixture, int piecemeal)
+{
+    size_t i;
+    unsigned long err;
+    char *sent;
+    long sent_len;
+    int ret = -1, found_ka = 0, chunk;
+
+    (void)BIO_reset(wbio);
+    if (!TEST_true(OSSL_HTTP_REQ_CTX_set_request_line(rctx, 0 /* GET */,
+            NULL, NULL, RPATH))
+        || !TEST_true(OSSL_HTTP_REQ_CTX_set_expected(rctx, fixture->expected_ct,
+            fixture->expect_asn1, 0, fixture->keep_alive))
+        /* adds the Connection header if keep-alive is requested */
+        || !TEST_true(OSSL_HTTP_REQ_CTX_set1_req(rctx, NULL, NULL, NULL)))
+        return 0;
+    OSSL_HTTP_REQ_CTX_set_max_response_hdr_lines(rctx, fixture->max_hdr_lines);
+
+    /* send GET request; no response available yet, so result should request retry */
+    if (!TEST_int_eq(OSSL_HTTP_REQ_CTX_nbio(rctx), -1))
+        return 0;
+
+    sent_len = BIO_get_mem_data(wbio, &sent);
+    for (i = 0; sent_len > 0 && i + strlen(KEEP_ALIVE_HDR) <= (size_t)sent_len; i++)
+        if (strncmp(sent + i, RESP(KEEP_ALIVE_HDR)) == 0)
+            found_ka = 1;
+    if (!TEST_int_eq(found_ka, fixture->keep_alive != 0))
+        return 0;
+
+    ERR_clear_error();
+    /* core of the test: simulate full or piecemeal response transfer */
+    if (!TEST_size_t_le(fixture->resp_len, INT_MAX))
+        return 0;
+    chunk = piecemeal ? 1 : (int)fixture->resp_len;
+    for (i = 0; i < fixture->resp_len && ret == -1; i += (size_t)chunk) {
+        /* provide to the buffer part/all of the response */
+        if (!TEST_int_eq(BIO_write(BIO_next(rbio), fixture->resp + i, chunk), chunk))
+            return 0;
+        ret = OSSL_HTTP_REQ_CTX_nbio(rctx);
+    }
+
+    if (!TEST_int_eq(ret, fixture->exp_ret))
+        return 0;
+    if (fixture->exp_ret == 0) {
+        err = ERR_peek_last_error();
+        if (!TEST_int_eq(ERR_GET_LIB(err), ERR_LIB_HTTP)
+            || !TEST_int_eq(ERR_GET_REASON(err), fixture->exp_reason))
+            return 0;
+    } else if (!TEST_int_eq(OSSL_HTTP_is_alive(rctx), fixture->expect_alive)
+        || (fixture->expect_asn1
+            && !TEST_size_t_eq(OSSL_HTTP_REQ_CTX_get_resp_len(rctx),
+                fixture->expected_body_len))) {
+        return 0;
+    }
+    return 1;
+}
+
+/**
+ * @brief Create an OSSL_HTTP_REQ_CTX on mem BIOs for non-blocking I/O.
+ * Reading from the response BIO signals retry while no data is available.
+ * BIO_gets() on a mem BIO does not signal retry, hence the buffer BIO on
+ * top of it. Response data is written to BIO_next(*rbio).
+ * @param wbio set to the BIO receiving the request, freed by the caller
+ * @param rbio set to the BIO chain providing the response, freed by the
+ * caller with BIO_free_all()
+ * @returns the new context, or NULL on error
+ */
+static OSSL_HTTP_REQ_CTX *new_nbio_ctx(BIO **wbio, BIO **rbio)
+{
+    BIO *in = NULL;
+
+    *rbio = NULL;
+    if (!TEST_ptr(*wbio = BIO_new(BIO_s_mem()))
+        || !TEST_ptr(*rbio = BIO_new(BIO_f_buffer()))
+        || !TEST_ptr(in = BIO_new(BIO_s_mem())))
+        return NULL;
+    BIO_set_mem_eof_return(in, -1);
+    *rbio = BIO_push(*rbio, in);
+    return OSSL_HTTP_REQ_CTX_new(*wbio, *rbio, 1024);
+}
+
+/**
+ * Responses whose outcome must not depend on how they are split across
+ * calls to OSSL_HTTP_REQ_CTX_nbio() that return -1.
+ */
+static const RESP_TEST_FIXTURE split_resp_tests[] = {
+    /* 4 response header lines (including the empty one) exceed limit 2 */
+    { RESP("HTTP/1.0 200 OK\r\n"
+           "X-Test-A: 1\r\n"
+           "X-Test-B: 2\r\n"
+           "\r\n"),
+        .max_hdr_lines = 2,
+        .exp_ret = 0,
+        .exp_reason = HTTP_R_RESPONSE_TOO_MANY_HDRLINES },
+    /*-
+     * Text Content-Type on status 200 while ASN.1 is expected.
+     * The piecemeal case checks preservation of the status code across calls.
+     * If it were reset to 0, the expected Content-Type mismatch would not
+     * be reported and the test would fail.
+     */
+    { RESP("HTTP/1.1 200 OK\r\n"
+           "Content-Type: text/plain\r\n"
+           "\r\n"),
+        .expect_asn1 = 1,
+        .exp_ret = 0,
+        .exp_reason = HTTP_R_CONTENT_TYPE_MISMATCH },
+    /* expected Content-Type was given before end of headers */
+    { RESP("HTTP/1.0 200 OK\r\n"
+           "Content-Type: text/plain\r\n"
+           "X-Test: 1\r\n"
+           "\r\n"),
+        .expected_ct = "text/plain",
+        .exp_ret = 1,
+        .expect_alive = 0 },
+    /* HTTP/1.1 implies keep-alive, as required by the client */
+    { RESP("HTTP/1.1 200 OK\r\n"
+           "Content-Type: text/plain\r\n"
+           "\r\n"),
+        .keep_alive = 2,
+        .exp_ret = 1,
+        .expect_alive = 1 },
+    /* ASN.1 response body is read after the headers */
+    { RESP("HTTP/1.0 200 OK\r\n"
+           "Content-Type: application/octet-stream\r\n"
+           "Content-Length: 2\r\n"
+           "\r\n"
+           "\x30\x00"),
+        .expect_asn1 = 1,
+        .exp_ret = 1,
+        .expected_body_len = 2,
+        .expect_alive = 0 },
+    /* server persistence does not enable keep-alive without a request */
+    { RESP("HTTP/1.1 200 OK\r\n"
+           "Content-Length: 0\r\n"
+           "\r\n"),
+        .keep_alive = 0,
+        .exp_ret = 1,
+        .expect_alive = 0 },
+    /* the server may decline keep-alive when it is only preferred */
+    { RESP("HTTP/1.1 200 OK\r\n"
+           "Connection: close\r\n"
+           "Content-Length: 0\r\n"
+           "\r\n"),
+        .keep_alive = 1,
+        .exp_ret = 1,
+        .expect_alive = 0 },
+};
+
+/**
+ * @brief Receive split_resp_tests[idx / 2], all at once if idx is even,
+ * else one byte at a time.
+ * @param idx test iteration index
+ * @returns 1 on success, else 0
+ */
+static int test_http_split_resp(int idx)
+{
+    int piecemeal = idx % 2;
+    BIO *wbio = NULL, *rbio = NULL;
+    OSSL_HTTP_REQ_CTX *rctx = new_nbio_ctx(&wbio, &rbio);
+    int res;
+
+    TEST_info("response #%d, %s", idx / 2,
+        piecemeal ? "piecemeal" : "at once");
+    res = rctx != NULL
+        && exchange(rctx, wbio, rbio, &split_resp_tests[idx / 2], piecemeal);
+
+    ERR_clear_error();
+    OSSL_HTTP_REQ_CTX_free(rctx);
+    BIO_free(wbio);
+    BIO_free_all(rbio);
+    return res;
+}
+
+/**
+ * Pairs of responses received in order on the same OSSL_HTTP_REQ_CTX over
+ * a persistent connection, the first response being complete before the
+ * second request is sent. The outcome for each response must not depend
+ * on the one before it.
+ */
+static const RESP_TEST_FIXTURE reuse_resp_tests[][2] = {
+    /* 4 response header lines each, 8 in total, limit 4 */
+    {
+        { RESP("HTTP/1.1 200 OK\r\n"
+               "Content-Type: text/plain\r\n"
+               "Content-Length: 0\r\n"
+               "\r\n"),
+            .max_hdr_lines = 4,
+            .keep_alive = 2,
+            .exp_ret = 1,
+            .expect_alive = 1 },
+        { RESP("HTTP/1.1 200 OK\r\n"
+               "Content-Type: text/plain\r\n"
+               "Content-Length: 0\r\n"
+               "\r\n"),
+            .max_hdr_lines = 4,
+            .keep_alive = 2,
+            .exp_ret = 1,
+            .expect_alive = 1 },
+    },
+    /*-
+     * Both requests expect text/plain, but only the first response provides it.
+     * The second must fail if its own Content-Type header is missing.
+     */
+    {
+        { RESP("HTTP/1.1 200 OK\r\n"
+               "Content-Type: text/plain\r\n"
+               "Content-Length: 0\r\n"
+               "\r\n"),
+            .expected_ct = "text/plain",
+            .keep_alive = 2,
+            .exp_ret = 1,
+            .expect_alive = 1 },
+        { RESP("HTTP/1.1 200 OK\r\n"
+               "Content-Length: 0\r\n"
+               "\r\n"),
+            .expected_ct = "text/plain",
+            .keep_alive = 2,
+            .exp_ret = 0,
+            .exp_reason = HTTP_R_MISSING_CONTENT_TYPE },
+    },
+};
+
+/**
+ * @brief Receive the pair reuse_resp_tests[idx / 2] on one context, each
+ * response all at once if idx is even, else one byte at a time.
+ * @param idx test iteration index
+ * @returns 1 on success, else 0
+ */
+static int test_http_reuse_resp(int idx)
+{
+    int piecemeal = idx % 2;
+    BIO *wbio = NULL, *rbio = NULL;
+    OSSL_HTTP_REQ_CTX *rctx = new_nbio_ctx(&wbio, &rbio);
+    int res;
+
+    TEST_info("response pair #%d, %s", idx / 2,
+        piecemeal ? "piecemeal" : "at once");
+    res = rctx != NULL
+        && exchange(rctx, wbio, rbio, &reuse_resp_tests[idx / 2][0], piecemeal)
+        && TEST_int_eq(BIO_pending(rbio), 0)
+        && exchange(rctx, wbio, rbio, &reuse_resp_tests[idx / 2][1], piecemeal);
+
+    ERR_clear_error();
+    OSSL_HTTP_REQ_CTX_free(rctx);
+    BIO_free(wbio);
+    BIO_free_all(rbio);
+    return res;
+}
+
 static int test_http_adapt_proxy_empty_server(void)
 {
     const char *proxy = "http://proxy.local:8080";
@@ -781,6 +1075,8 @@ int setup_tests(void)
     ADD_TEST(test_hdr_resp_hdr_limit_none);
     ADD_TEST(test_hdr_resp_hdr_limit_short);
     ADD_TEST(test_hdr_resp_hdr_limit_256);
+    ADD_ALL_TESTS(test_http_split_resp, 2 * OSSL_NELEM(split_resp_tests));
+    ADD_ALL_TESTS(test_http_reuse_resp, 2 * OSSL_NELEM(reuse_resp_tests));
     ADD_TEST(test_http_adapt_proxy_empty_server);
     return 1;
 }