From 79d7b987d4df8ddeac3b2f965da6025b14243cb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Sch=C3=A4rtl?= Date: Mon, 29 Jan 2024 11:32:49 +0100 Subject: [PATCH] Handle missing Content-Length in client This fixes a problem when trying to connect to the globalsign/est test server implementation. [1] https://github.com/globalsign/est --- src/est/est_client_http.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/est/est_client_http.c b/src/est/est_client_http.c index 830e6e3..9f746d4 100644 --- a/src/est/est_client_http.c +++ b/src/est/est_client_http.c @@ -1457,8 +1457,7 @@ static EST_ERROR est_io_check_http_hdrs (HTTP_HEADER *hdrs, int hdr_cnt, return EST_ERR_HTTP_UNSUPPORTED; } if (content_length_present == 0) { - EST_LOG_ERR("Missing HTTP content length header"); - return EST_ERR_HTTP_UNSUPPORTED; + EST_LOG_INFO("Missing HTTP content length header"); } return EST_ERR_NONE; @@ -1557,6 +1556,7 @@ EST_ERROR est_io_get_response_internal (EST_CTX *ctx, SSL *ssl, EST_OPERATION op unsigned char *raw_buf = NULL, *payload_skip_extra = NULL; unsigned char *payload, *payload_buf; int raw_len = 0; + int body_len; errno_t safec_rc; @@ -1763,6 +1763,15 @@ EST_ERROR est_io_get_response_internal (EST_CTX *ctx, SSL *ssl, EST_OPERATION op */ rv = est_io_check_http_hdrs(hdrs, hdr_cnt, op, cert_buf_len); if (rv == EST_ERR_NONE) { + body_len = (raw_len + raw_buf) - payload; + + if (*cert_buf_len == -1) { + /* + * Connection is being closed. Allocate just enough + * for the body we have received. + */ + *cert_buf_len = body_len; + } EST_LOG_INFO("HTTP Content len=%d", *cert_buf_len); @@ -1778,7 +1787,7 @@ EST_ERROR est_io_get_response_internal (EST_CTX *ctx, SSL *ssl, EST_OPERATION op rv = EST_ERR_HTTP_UNSUPPORTED; *cert_buf_len = 0; *cert_buf = NULL; - } else if (*cert_buf_len != (raw_len + raw_buf) - payload) { + } else if (*cert_buf_len != body_len) { EST_LOG_ERR( "Content Length (%d) and body length (%d) mismatch.", *cert_buf_len, (raw_len + raw_buf) - payload);