krb5: use dynbuf

Closes #13568
This commit is contained in:
Daniel Stenberg 2024-05-08 15:20:23 +02:00
parent a95fd86404
commit 0f4c439fc7
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 33 additions and 28 deletions

View File

@ -524,24 +524,33 @@ static CURLcode read_data(struct Curl_easy *data, int sockindex,
return result; return result;
if(len) { if(len) {
/* only realloc if there was a length */
len = ntohl(len); len = ntohl(len);
if(len > CURL_MAX_INPUT_LENGTH) if(len > CURL_MAX_INPUT_LENGTH)
len = 0; return CURLE_TOO_LARGE;
else
buf->data = Curl_saferealloc(buf->data, len);
}
if(!len || !buf->data)
return CURLE_OUT_OF_MEMORY;
result = socket_read(data, sockindex, buf->data, len); Curl_dyn_reset(&buf->buf);
if(result) }
return result; else
nread = conn->mech->decode(conn->app_data, buf->data, len, return CURLE_RECV_ERROR;
conn->data_prot, conn);
do {
char buffer[1024];
nread = CURLMIN(len, (int)sizeof(buffer));
result = socket_read(data, sockindex, buffer, nread);
if(result)
return result;
result = Curl_dyn_addn(&buf->buf, buffer, nread);
if(result)
return result;
len -= nread;
} while(len);
/* this decodes the dynbuf *in place* */
nread = conn->mech->decode(conn->app_data,
Curl_dyn_ptr(&buf->buf),
len, conn->data_prot, conn);
if(nread < 0) if(nread < 0)
return CURLE_RECV_ERROR; return CURLE_RECV_ERROR;
buf->size = (size_t)nread; Curl_dyn_setlen(&buf->buf, nread);
buf->index = 0; buf->index = 0;
return CURLE_OK; return CURLE_OK;
} }
@ -549,9 +558,10 @@ static CURLcode read_data(struct Curl_easy *data, int sockindex,
static size_t static size_t
buffer_read(struct krb5buffer *buf, void *data, size_t len) buffer_read(struct krb5buffer *buf, void *data, size_t len)
{ {
if(buf->size - buf->index < len) size_t size = Curl_dyn_len(&buf->buf);
len = buf->size - buf->index; if(size - buf->index < len)
memcpy(data, (char *)buf->data + buf->index, len); len = size - buf->index;
memcpy(data, Curl_dyn_ptr(&buf->buf) + buf->index, len);
buf->index += len; buf->index += len;
return len; return len;
} }
@ -586,7 +596,7 @@ static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
while(len > 0) { while(len > 0) {
if(read_data(data, sockindex, &conn->in_buffer)) if(read_data(data, sockindex, &conn->in_buffer))
return -1; return -1;
if(conn->in_buffer.size == 0) { if(Curl_dyn_len(&conn->in_buffer.buf) == 0) {
if(bytes_read > 0) if(bytes_read > 0)
conn->in_buffer.eof_flag = 1; conn->in_buffer.eof_flag = 1;
return bytes_read; return bytes_read;
@ -835,6 +845,7 @@ static CURLcode choose_mech(struct Curl_easy *data, struct connectdata *conn)
mech->name); mech->name);
return CURLE_FAILED_INIT; return CURLE_FAILED_INIT;
} }
Curl_dyn_init(&conn->in_buffer.buf, CURL_MAX_INPUT_LENGTH);
} }
infof(data, "Trying mechanism %s...", mech->name); infof(data, "Trying mechanism %s...", mech->name);
@ -899,15 +910,10 @@ Curl_sec_end(struct connectdata *conn)
{ {
if(conn->mech && conn->mech->end) if(conn->mech && conn->mech->end)
conn->mech->end(conn->app_data); conn->mech->end(conn->app_data);
free(conn->app_data); Curl_safefree(conn->app_data);
conn->app_data = NULL; Curl_dyn_free(&conn->in_buffer.buf);
if(conn->in_buffer.data) { conn->in_buffer.index = 0;
free(conn->in_buffer.data); conn->in_buffer.eof_flag = 0;
conn->in_buffer.data = NULL;
conn->in_buffer.size = 0;
conn->in_buffer.index = 0;
conn->in_buffer.eof_flag = 0;
}
conn->sec_complete = 0; conn->sec_complete = 0;
conn->data_prot = PROT_CLEAR; conn->data_prot = PROT_CLEAR;
conn->mech = NULL; conn->mech = NULL;

View File

@ -241,8 +241,7 @@ typedef CURLcode (*Curl_datastream)(struct Curl_easy *data,
#ifdef HAVE_GSSAPI #ifdef HAVE_GSSAPI
/* Types needed for krb5-ftp connections */ /* Types needed for krb5-ftp connections */
struct krb5buffer { struct krb5buffer {
void *data; struct dynbuf buf;
size_t size;
size_t index; size_t index;
BIT(eof_flag); BIT(eof_flag);
}; };