| 1 |
/* |
| 2 |
* Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <errno.h> |
| 12 |
#include "bio_local.h" |
| 13 |
#include "internal/cryptlib.h" |
| 14 |
#include <openssl/evp.h> |
| 15 |
|
| 16 |
static int linebuffer_write(BIO *h, const char *buf, int num); |
| 17 |
static int linebuffer_read(BIO *h, char *buf, int size); |
| 18 |
static int linebuffer_puts(BIO *h, const char *str); |
| 19 |
static int linebuffer_gets(BIO *h, char *str, int size); |
| 20 |
static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
| 21 |
static int linebuffer_new(BIO *h); |
| 22 |
static int linebuffer_free(BIO *data); |
| 23 |
static long linebuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); |
| 24 |
|
| 25 |
/* A 10k maximum should be enough for most purposes */ |
| 26 |
#define DEFAULT_LINEBUFFER_SIZE 1024 * 10 |
| 27 |
|
| 28 |
/* #define DEBUG */ |
| 29 |
|
| 30 |
static const BIO_METHOD methods_linebuffer = { |
| 31 |
BIO_TYPE_LINEBUFFER, |
| 32 |
"linebuffer", |
| 33 |
bwrite_conv, |
| 34 |
linebuffer_write, |
| 35 |
bread_conv, |
| 36 |
linebuffer_read, |
| 37 |
linebuffer_puts, |
| 38 |
linebuffer_gets, |
| 39 |
linebuffer_ctrl, |
| 40 |
linebuffer_new, |
| 41 |
linebuffer_free, |
| 42 |
linebuffer_callback_ctrl, |
| 43 |
}; |
| 44 |
|
| 45 |
const BIO_METHOD *BIO_f_linebuffer(void) |
| 46 |
{ |
| 47 |
return &methods_linebuffer; |
| 48 |
} |
| 49 |
|
| 50 |
typedef struct bio_linebuffer_ctx_struct { |
| 51 |
char *obuf; /* the output char array */ |
| 52 |
int obuf_size; /* how big is the output buffer */ |
| 53 |
int obuf_len; /* how many bytes are in it */ |
| 54 |
} BIO_LINEBUFFER_CTX; |
| 55 |
|
| 56 |
static int linebuffer_new(BIO *bi) |
| 57 |
{ |
| 58 |
BIO_LINEBUFFER_CTX *ctx; |
| 59 |
|
| 60 |
if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) |
| 61 |
return 0; |
| 62 |
ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE); |
| 63 |
if (ctx->obuf == NULL) { |
| 64 |
OPENSSL_free(ctx); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE; |
| 68 |
ctx->obuf_len = 0; |
| 69 |
|
| 70 |
bi->init = 1; |
| 71 |
bi->ptr = (char *)ctx; |
| 72 |
bi->flags = 0; |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
static int linebuffer_free(BIO *a) |
| 77 |
{ |
| 78 |
BIO_LINEBUFFER_CTX *b; |
| 79 |
|
| 80 |
if (a == NULL) |
| 81 |
return 0; |
| 82 |
b = (BIO_LINEBUFFER_CTX *)a->ptr; |
| 83 |
OPENSSL_free(b->obuf); |
| 84 |
OPENSSL_free(a->ptr); |
| 85 |
a->ptr = NULL; |
| 86 |
a->init = 0; |
| 87 |
a->flags = 0; |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
static int linebuffer_read(BIO *b, char *out, int outl) |
| 92 |
{ |
| 93 |
int ret = 0; |
| 94 |
|
| 95 |
if (out == NULL) |
| 96 |
return 0; |
| 97 |
if (b->next_bio == NULL) |
| 98 |
return 0; |
| 99 |
ret = BIO_read(b->next_bio, out, outl); |
| 100 |
BIO_clear_retry_flags(b); |
| 101 |
BIO_copy_next_retry(b); |
| 102 |
return ret; |
| 103 |
} |
| 104 |
|
| 105 |
static int linebuffer_write(BIO *b, const char *in, int inl) |
| 106 |
{ |
| 107 |
int i, num = 0, foundnl; |
| 108 |
BIO_LINEBUFFER_CTX *ctx; |
| 109 |
|
| 110 |
if ((in == NULL) || (inl <= 0)) |
| 111 |
return 0; |
| 112 |
ctx = (BIO_LINEBUFFER_CTX *)b->ptr; |
| 113 |
if ((ctx == NULL) || (b->next_bio == NULL)) |
| 114 |
return 0; |
| 115 |
|
| 116 |
BIO_clear_retry_flags(b); |
| 117 |
|
| 118 |
do { |
| 119 |
const char *p; |
| 120 |
char c; |
| 121 |
|
| 122 |
for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) |
| 123 |
; |
| 124 |
if (c == '\n') { |
| 125 |
p++; |
| 126 |
foundnl = 1; |
| 127 |
} else |
| 128 |
foundnl = 0; |
| 129 |
|
| 130 |
/* |
| 131 |
* If a NL was found and we already have text in the save buffer, |
| 132 |
* concatenate them and write |
| 133 |
*/ |
| 134 |
while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len) |
| 135 |
&& ctx->obuf_len > 0) { |
| 136 |
int orig_olen = ctx->obuf_len; |
| 137 |
int llen = (int)(p - in); |
| 138 |
|
| 139 |
i = ctx->obuf_size - ctx->obuf_len; |
| 140 |
if (llen > 0) { |
| 141 |
if (i >= llen) { |
| 142 |
memcpy(&(ctx->obuf[ctx->obuf_len]), in, llen); |
| 143 |
ctx->obuf_len += llen; |
| 144 |
inl -= llen; |
| 145 |
num += llen; |
| 146 |
in = p; |
| 147 |
} else { |
| 148 |
memcpy(&(ctx->obuf[ctx->obuf_len]), in, i); |
| 149 |
ctx->obuf_len += i; |
| 150 |
inl -= i; |
| 151 |
in += i; |
| 152 |
num += i; |
| 153 |
} |
| 154 |
} |
| 155 |
i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len); |
| 156 |
if (i <= 0) { |
| 157 |
ctx->obuf_len = orig_olen; |
| 158 |
BIO_copy_next_retry(b); |
| 159 |
|
| 160 |
if (i < 0) |
| 161 |
return ((num > 0) ? num : i); |
| 162 |
if (i == 0) |
| 163 |
return num; |
| 164 |
} |
| 165 |
if (i < ctx->obuf_len) |
| 166 |
memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i); |
| 167 |
ctx->obuf_len -= i; |
| 168 |
} |
| 169 |
|
| 170 |
/* |
| 171 |
* Now that the save buffer is emptied, let's write the input buffer |
| 172 |
* if a NL was found and there is anything to write. |
| 173 |
*/ |
| 174 |
if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) { |
| 175 |
i = BIO_write(b->next_bio, in, (int)(p - in)); |
| 176 |
if (i <= 0) { |
| 177 |
BIO_copy_next_retry(b); |
| 178 |
if (i < 0) |
| 179 |
return ((num > 0) ? num : i); |
| 180 |
if (i == 0) |
| 181 |
return num; |
| 182 |
} |
| 183 |
num += i; |
| 184 |
in += i; |
| 185 |
inl -= i; |
| 186 |
} |
| 187 |
} while (foundnl && inl > 0); |
| 188 |
/* |
| 189 |
* We've written as much as we can. The rest of the input buffer, if |
| 190 |
* any, is text that doesn't end with a NL and therefore we need to try |
| 191 |
* free up some space in our obuf so we can make forward progress. |
| 192 |
*/ |
| 193 |
while (inl > 0) { |
| 194 |
size_t avail = (size_t)ctx->obuf_size - (size_t)ctx->obuf_len; |
| 195 |
size_t to_copy; |
| 196 |
|
| 197 |
if (avail == 0) { |
| 198 |
/* Flush buffered data to make room */ |
| 199 |
i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len); |
| 200 |
if (i <= 0) { |
| 201 |
BIO_copy_next_retry(b); |
| 202 |
return num > 0 ? num : i; |
| 203 |
} |
| 204 |
if (i < ctx->obuf_len) |
| 205 |
memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i); |
| 206 |
ctx->obuf_len -= i; |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
to_copy = inl > (int)avail ? avail : (size_t)inl; |
| 211 |
memcpy(&(ctx->obuf[ctx->obuf_len]), in, to_copy); |
| 212 |
ctx->obuf_len += (int)to_copy; |
| 213 |
in += to_copy; |
| 214 |
inl -= (int)to_copy; |
| 215 |
num += (int)to_copy; |
| 216 |
} |
| 217 |
|
| 218 |
return num; |
| 219 |
} |
| 220 |
|
| 221 |
static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr) |
| 222 |
{ |
| 223 |
BIO *dbio; |
| 224 |
BIO_LINEBUFFER_CTX *ctx; |
| 225 |
long ret = 1; |
| 226 |
char *p; |
| 227 |
int r; |
| 228 |
int obs; |
| 229 |
|
| 230 |
ctx = (BIO_LINEBUFFER_CTX *)b->ptr; |
| 231 |
|
| 232 |
switch (cmd) { |
| 233 |
case BIO_CTRL_RESET: |
| 234 |
ctx->obuf_len = 0; |
| 235 |
if (b->next_bio == NULL) |
| 236 |
return 0; |
| 237 |
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 238 |
break; |
| 239 |
case BIO_CTRL_INFO: |
| 240 |
ret = (long)ctx->obuf_len; |
| 241 |
break; |
| 242 |
case BIO_CTRL_WPENDING: |
| 243 |
ret = (long)ctx->obuf_len; |
| 244 |
if (ret == 0) { |
| 245 |
if (b->next_bio == NULL) |
| 246 |
return 0; |
| 247 |
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 248 |
} |
| 249 |
break; |
| 250 |
case BIO_C_SET_BUFF_SIZE: |
| 251 |
if (num > INT_MAX) |
| 252 |
return 0; |
| 253 |
obs = (int)num; |
| 254 |
p = ctx->obuf; |
| 255 |
if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) { |
| 256 |
p = OPENSSL_malloc((size_t)obs); |
| 257 |
if (p == NULL) |
| 258 |
return 0; |
| 259 |
} |
| 260 |
if (ctx->obuf != p) { |
| 261 |
if (ctx->obuf_len > obs) { |
| 262 |
ctx->obuf_len = obs; |
| 263 |
} |
| 264 |
memcpy(p, ctx->obuf, ctx->obuf_len); |
| 265 |
OPENSSL_free(ctx->obuf); |
| 266 |
ctx->obuf = p; |
| 267 |
ctx->obuf_size = obs; |
| 268 |
} |
| 269 |
break; |
| 270 |
case BIO_C_DO_STATE_MACHINE: |
| 271 |
if (b->next_bio == NULL) |
| 272 |
return 0; |
| 273 |
BIO_clear_retry_flags(b); |
| 274 |
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 275 |
BIO_copy_next_retry(b); |
| 276 |
break; |
| 277 |
|
| 278 |
case BIO_CTRL_FLUSH: |
| 279 |
if (b->next_bio == NULL) |
| 280 |
return 0; |
| 281 |
if (ctx->obuf_len <= 0) { |
| 282 |
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 283 |
BIO_copy_next_retry(b); |
| 284 |
break; |
| 285 |
} |
| 286 |
|
| 287 |
for (;;) { |
| 288 |
BIO_clear_retry_flags(b); |
| 289 |
if (ctx->obuf_len > 0) { |
| 290 |
r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len); |
| 291 |
BIO_copy_next_retry(b); |
| 292 |
if (r <= 0) |
| 293 |
return (long)r; |
| 294 |
if (r < ctx->obuf_len) |
| 295 |
memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r); |
| 296 |
ctx->obuf_len -= r; |
| 297 |
} else { |
| 298 |
ctx->obuf_len = 0; |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 303 |
BIO_copy_next_retry(b); |
| 304 |
break; |
| 305 |
case BIO_CTRL_DUP: |
| 306 |
dbio = (BIO *)ptr; |
| 307 |
if (BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0) |
| 308 |
ret = 0; |
| 309 |
break; |
| 310 |
default: |
| 311 |
if (b->next_bio == NULL) |
| 312 |
return 0; |
| 313 |
ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 314 |
break; |
| 315 |
} |
| 316 |
return ret; |
| 317 |
} |
| 318 |
|
| 319 |
static long linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
| 320 |
{ |
| 321 |
if (b->next_bio == NULL) |
| 322 |
return 0; |
| 323 |
return BIO_callback_ctrl(b->next_bio, cmd, fp); |
| 324 |
} |
| 325 |
|
| 326 |
static int linebuffer_gets(BIO *b, char *buf, int size) |
| 327 |
{ |
| 328 |
if (b->next_bio == NULL) |
| 329 |
return 0; |
| 330 |
return BIO_gets(b->next_bio, buf, size); |
| 331 |
} |
| 332 |
|
| 333 |
static int linebuffer_puts(BIO *b, const char *str) |
| 334 |
{ |
| 335 |
size_t len = strlen(str); |
| 336 |
|
| 337 |
if (len > INT_MAX) |
| 338 |
return -1; |
| 339 |
return linebuffer_write(b, str, (int)len); |
| 340 |
} |