/src/openssl30/crypto/evp/bio_enc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 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 | | #define OPENSSL_SUPPRESS_DEPRECATED /* for BIO_get_callback */ |
11 | | |
12 | | #include <stdio.h> |
13 | | #include <errno.h> |
14 | | #include "internal/cryptlib.h" |
15 | | #include <openssl/buffer.h> |
16 | | #include <openssl/evp.h> |
17 | | #include "internal/bio.h" |
18 | | |
19 | | static int enc_write(BIO *h, const char *buf, int num); |
20 | | static int enc_read(BIO *h, char *buf, int size); |
21 | | static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
22 | | static int enc_new(BIO *h); |
23 | | static int enc_free(BIO *data); |
24 | | static long enc_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fps); |
25 | 0 | #define ENC_BLOCK_SIZE (1024*4) |
26 | 0 | #define ENC_MIN_CHUNK (256) |
27 | 0 | #define BUF_OFFSET (ENC_MIN_CHUNK + EVP_MAX_BLOCK_LENGTH) |
28 | | |
29 | | typedef struct enc_struct { |
30 | | int buf_len; |
31 | | int buf_off; |
32 | | int cont; /* <= 0 when finished */ |
33 | | int finished; |
34 | | int ok; /* bad decrypt */ |
35 | | EVP_CIPHER_CTX *cipher; |
36 | | unsigned char *read_start, *read_end; |
37 | | /* |
38 | | * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return |
39 | | * up to a block more data than is presented to it |
40 | | */ |
41 | | unsigned char buf[BUF_OFFSET + ENC_BLOCK_SIZE]; |
42 | | } BIO_ENC_CTX; |
43 | | |
44 | | static const BIO_METHOD methods_enc = { |
45 | | BIO_TYPE_CIPHER, |
46 | | "cipher", |
47 | | bwrite_conv, |
48 | | enc_write, |
49 | | bread_conv, |
50 | | enc_read, |
51 | | NULL, /* enc_puts, */ |
52 | | NULL, /* enc_gets, */ |
53 | | enc_ctrl, |
54 | | enc_new, |
55 | | enc_free, |
56 | | enc_callback_ctrl, |
57 | | }; |
58 | | |
59 | | const BIO_METHOD *BIO_f_cipher(void) |
60 | 0 | { |
61 | 0 | return &methods_enc; |
62 | 0 | } |
63 | | |
64 | | static int enc_new(BIO *bi) |
65 | 0 | { |
66 | 0 | BIO_ENC_CTX *ctx; |
67 | |
|
68 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) { |
69 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
70 | 0 | return 0; |
71 | 0 | } |
72 | | |
73 | 0 | ctx->cipher = EVP_CIPHER_CTX_new(); |
74 | 0 | if (ctx->cipher == NULL) { |
75 | 0 | OPENSSL_free(ctx); |
76 | 0 | return 0; |
77 | 0 | } |
78 | 0 | ctx->cont = 1; |
79 | 0 | ctx->ok = 1; |
80 | 0 | ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]); |
81 | 0 | BIO_set_data(bi, ctx); |
82 | 0 | BIO_set_init(bi, 1); |
83 | |
|
84 | 0 | return 1; |
85 | 0 | } |
86 | | |
87 | | static int enc_free(BIO *a) |
88 | 0 | { |
89 | 0 | BIO_ENC_CTX *b; |
90 | |
|
91 | 0 | if (a == NULL) |
92 | 0 | return 0; |
93 | | |
94 | 0 | b = BIO_get_data(a); |
95 | 0 | if (b == NULL) |
96 | 0 | return 0; |
97 | | |
98 | 0 | EVP_CIPHER_CTX_free(b->cipher); |
99 | 0 | OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX)); |
100 | 0 | BIO_set_data(a, NULL); |
101 | 0 | BIO_set_init(a, 0); |
102 | |
|
103 | 0 | return 1; |
104 | 0 | } |
105 | | |
106 | | static int enc_read(BIO *b, char *out, int outl) |
107 | 0 | { |
108 | 0 | int ret = 0, i, blocksize; |
109 | 0 | BIO_ENC_CTX *ctx; |
110 | 0 | BIO *next; |
111 | |
|
112 | 0 | if (out == NULL) |
113 | 0 | return 0; |
114 | 0 | ctx = BIO_get_data(b); |
115 | |
|
116 | 0 | next = BIO_next(b); |
117 | 0 | if ((ctx == NULL) || (next == NULL)) |
118 | 0 | return 0; |
119 | | |
120 | | /* First check if there are bytes decoded/encoded */ |
121 | 0 | if (ctx->buf_len > 0) { |
122 | 0 | i = ctx->buf_len - ctx->buf_off; |
123 | 0 | if (i > outl) |
124 | 0 | i = outl; |
125 | 0 | memcpy(out, &(ctx->buf[ctx->buf_off]), i); |
126 | 0 | ret = i; |
127 | 0 | out += i; |
128 | 0 | outl -= i; |
129 | 0 | ctx->buf_off += i; |
130 | 0 | if (ctx->buf_len == ctx->buf_off) { |
131 | 0 | ctx->buf_len = 0; |
132 | 0 | ctx->buf_off = 0; |
133 | 0 | } |
134 | 0 | } |
135 | |
|
136 | 0 | blocksize = EVP_CIPHER_CTX_get_block_size(ctx->cipher); |
137 | 0 | if (blocksize == 1) |
138 | 0 | blocksize = 0; |
139 | | |
140 | | /* |
141 | | * At this point, we have room of outl bytes and an empty buffer, so we |
142 | | * should read in some more. |
143 | | */ |
144 | |
|
145 | 0 | while (outl > 0) { |
146 | 0 | if (ctx->cont <= 0) |
147 | 0 | break; |
148 | | |
149 | 0 | if (ctx->read_start == ctx->read_end) { /* time to read more data */ |
150 | 0 | ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]); |
151 | 0 | i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE); |
152 | 0 | if (i > 0) |
153 | 0 | ctx->read_end += i; |
154 | 0 | } else { |
155 | 0 | i = ctx->read_end - ctx->read_start; |
156 | 0 | } |
157 | |
|
158 | 0 | if (i <= 0) { |
159 | | /* Should be continue next time we are called? */ |
160 | 0 | if (!BIO_should_retry(next)) { |
161 | 0 | ctx->cont = i; |
162 | 0 | ctx->finished = 1; |
163 | 0 | i = EVP_CipherFinal_ex(ctx->cipher, |
164 | 0 | ctx->buf, &(ctx->buf_len)); |
165 | 0 | ctx->ok = i; |
166 | 0 | ctx->buf_off = 0; |
167 | 0 | } else { |
168 | 0 | ret = (ret == 0) ? i : ret; |
169 | 0 | break; |
170 | 0 | } |
171 | 0 | } else { |
172 | 0 | if (outl > ENC_MIN_CHUNK) { |
173 | | /* |
174 | | * Depending on flags block cipher decrypt can write |
175 | | * one extra block and then back off, i.e. output buffer |
176 | | * has to accommodate extra block... |
177 | | */ |
178 | 0 | int j = outl - blocksize, buf_len; |
179 | |
|
180 | 0 | if (!EVP_CipherUpdate(ctx->cipher, |
181 | 0 | (unsigned char *)out, &buf_len, |
182 | 0 | ctx->read_start, i > j ? j : i)) { |
183 | 0 | BIO_clear_retry_flags(b); |
184 | 0 | return 0; |
185 | 0 | } |
186 | 0 | ret += buf_len; |
187 | 0 | out += buf_len; |
188 | 0 | outl -= buf_len; |
189 | |
|
190 | 0 | if ((i -= j) <= 0) { |
191 | 0 | ctx->read_start = ctx->read_end; |
192 | 0 | continue; |
193 | 0 | } |
194 | 0 | ctx->read_start += j; |
195 | 0 | } |
196 | 0 | if (i > ENC_MIN_CHUNK) |
197 | 0 | i = ENC_MIN_CHUNK; |
198 | 0 | if (!EVP_CipherUpdate(ctx->cipher, |
199 | 0 | ctx->buf, &ctx->buf_len, |
200 | 0 | ctx->read_start, i)) { |
201 | 0 | BIO_clear_retry_flags(b); |
202 | 0 | ctx->ok = 0; |
203 | 0 | return 0; |
204 | 0 | } |
205 | 0 | ctx->read_start += i; |
206 | 0 | ctx->cont = 1; |
207 | | /* |
208 | | * Note: it is possible for EVP_CipherUpdate to decrypt zero |
209 | | * bytes because this is or looks like the final block: if this |
210 | | * happens we should retry and either read more data or decrypt |
211 | | * the final block |
212 | | */ |
213 | 0 | if (ctx->buf_len == 0) |
214 | 0 | continue; |
215 | 0 | } |
216 | | |
217 | 0 | if (ctx->buf_len <= outl) |
218 | 0 | i = ctx->buf_len; |
219 | 0 | else |
220 | 0 | i = outl; |
221 | 0 | if (i <= 0) |
222 | 0 | break; |
223 | 0 | memcpy(out, ctx->buf, i); |
224 | 0 | ret += i; |
225 | 0 | ctx->buf_off = i; |
226 | 0 | outl -= i; |
227 | 0 | out += i; |
228 | 0 | } |
229 | | |
230 | 0 | BIO_clear_retry_flags(b); |
231 | 0 | BIO_copy_next_retry(b); |
232 | 0 | return ((ret == 0) ? ctx->cont : ret); |
233 | 0 | } |
234 | | |
235 | | static int enc_write(BIO *b, const char *in, int inl) |
236 | 0 | { |
237 | 0 | int ret = 0, n, i; |
238 | 0 | BIO_ENC_CTX *ctx; |
239 | 0 | BIO *next; |
240 | |
|
241 | 0 | ctx = BIO_get_data(b); |
242 | 0 | next = BIO_next(b); |
243 | 0 | if ((ctx == NULL) || (next == NULL)) |
244 | 0 | return 0; |
245 | | |
246 | 0 | ret = inl; |
247 | |
|
248 | 0 | BIO_clear_retry_flags(b); |
249 | 0 | n = ctx->buf_len - ctx->buf_off; |
250 | 0 | while (n > 0) { |
251 | 0 | i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n); |
252 | 0 | if (i <= 0) { |
253 | 0 | BIO_copy_next_retry(b); |
254 | 0 | return i; |
255 | 0 | } |
256 | 0 | ctx->buf_off += i; |
257 | 0 | n -= i; |
258 | 0 | } |
259 | | /* at this point all pending data has been written */ |
260 | | |
261 | 0 | if ((in == NULL) || (inl <= 0)) |
262 | 0 | return 0; |
263 | | |
264 | 0 | ctx->buf_off = 0; |
265 | 0 | while (inl > 0) { |
266 | 0 | n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl; |
267 | 0 | if (!EVP_CipherUpdate(ctx->cipher, |
268 | 0 | ctx->buf, &ctx->buf_len, |
269 | 0 | (const unsigned char *)in, n)) { |
270 | 0 | BIO_clear_retry_flags(b); |
271 | 0 | ctx->ok = 0; |
272 | 0 | return 0; |
273 | 0 | } |
274 | 0 | inl -= n; |
275 | 0 | in += n; |
276 | |
|
277 | 0 | ctx->buf_off = 0; |
278 | 0 | n = ctx->buf_len; |
279 | 0 | while (n > 0) { |
280 | 0 | i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n); |
281 | 0 | if (i <= 0) { |
282 | 0 | BIO_copy_next_retry(b); |
283 | 0 | return (ret == inl) ? i : ret - inl; |
284 | 0 | } |
285 | 0 | n -= i; |
286 | 0 | ctx->buf_off += i; |
287 | 0 | } |
288 | 0 | ctx->buf_len = 0; |
289 | 0 | ctx->buf_off = 0; |
290 | 0 | } |
291 | 0 | BIO_copy_next_retry(b); |
292 | 0 | return ret; |
293 | 0 | } |
294 | | |
295 | | static long enc_ctrl(BIO *b, int cmd, long num, void *ptr) |
296 | 0 | { |
297 | 0 | BIO *dbio; |
298 | 0 | BIO_ENC_CTX *ctx, *dctx; |
299 | 0 | long ret = 1; |
300 | 0 | int i; |
301 | 0 | EVP_CIPHER_CTX **c_ctx; |
302 | 0 | BIO *next; |
303 | 0 | int pend; |
304 | |
|
305 | 0 | ctx = BIO_get_data(b); |
306 | 0 | next = BIO_next(b); |
307 | 0 | if (ctx == NULL) |
308 | 0 | return 0; |
309 | | |
310 | 0 | switch (cmd) { |
311 | 0 | case BIO_CTRL_RESET: |
312 | 0 | ctx->ok = 1; |
313 | 0 | ctx->finished = 0; |
314 | 0 | if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL, |
315 | 0 | EVP_CIPHER_CTX_is_encrypting(ctx->cipher))) |
316 | 0 | return 0; |
317 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
318 | 0 | break; |
319 | 0 | case BIO_CTRL_EOF: /* More to read */ |
320 | 0 | if (ctx->cont <= 0) |
321 | 0 | ret = 1; |
322 | 0 | else |
323 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
324 | 0 | break; |
325 | 0 | case BIO_CTRL_WPENDING: |
326 | 0 | ret = ctx->buf_len - ctx->buf_off; |
327 | 0 | if (ret <= 0) |
328 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
329 | 0 | break; |
330 | 0 | case BIO_CTRL_PENDING: /* More to read in buffer */ |
331 | 0 | ret = ctx->buf_len - ctx->buf_off; |
332 | 0 | if (ret <= 0) |
333 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
334 | 0 | break; |
335 | 0 | case BIO_CTRL_FLUSH: |
336 | | /* do a final write */ |
337 | 0 | again: |
338 | 0 | while (ctx->buf_len != ctx->buf_off) { |
339 | 0 | pend = ctx->buf_len - ctx->buf_off; |
340 | 0 | i = enc_write(b, NULL, 0); |
341 | | /* |
342 | | * i should never be > 0 here because we didn't ask to write any |
343 | | * new data. We stop if we get an error or we failed to make any |
344 | | * progress writing pending data. |
345 | | */ |
346 | 0 | if (i < 0 || (ctx->buf_len - ctx->buf_off) == pend) |
347 | 0 | return i; |
348 | 0 | } |
349 | | |
350 | 0 | if (!ctx->finished) { |
351 | 0 | ctx->finished = 1; |
352 | 0 | ctx->buf_off = 0; |
353 | 0 | ret = EVP_CipherFinal_ex(ctx->cipher, |
354 | 0 | (unsigned char *)ctx->buf, |
355 | 0 | &(ctx->buf_len)); |
356 | 0 | ctx->ok = (int)ret; |
357 | 0 | if (ret <= 0) |
358 | 0 | break; |
359 | | |
360 | | /* push out the bytes */ |
361 | 0 | goto again; |
362 | 0 | } |
363 | | |
364 | | /* Finally flush the underlying BIO */ |
365 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
366 | 0 | break; |
367 | 0 | case BIO_C_GET_CIPHER_STATUS: |
368 | 0 | ret = (long)ctx->ok; |
369 | 0 | break; |
370 | 0 | case BIO_C_DO_STATE_MACHINE: |
371 | 0 | BIO_clear_retry_flags(b); |
372 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
373 | 0 | BIO_copy_next_retry(b); |
374 | 0 | break; |
375 | 0 | case BIO_C_GET_CIPHER_CTX: |
376 | 0 | c_ctx = (EVP_CIPHER_CTX **)ptr; |
377 | 0 | *c_ctx = ctx->cipher; |
378 | 0 | BIO_set_init(b, 1); |
379 | 0 | break; |
380 | 0 | case BIO_CTRL_DUP: |
381 | 0 | dbio = (BIO *)ptr; |
382 | 0 | dctx = BIO_get_data(dbio); |
383 | 0 | dctx->cipher = EVP_CIPHER_CTX_new(); |
384 | 0 | if (dctx->cipher == NULL) |
385 | 0 | return 0; |
386 | 0 | ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher); |
387 | 0 | if (ret) |
388 | 0 | BIO_set_init(dbio, 1); |
389 | 0 | break; |
390 | 0 | default: |
391 | 0 | ret = BIO_ctrl(next, cmd, num, ptr); |
392 | 0 | break; |
393 | 0 | } |
394 | 0 | return ret; |
395 | 0 | } |
396 | | |
397 | | static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
398 | 0 | { |
399 | 0 | BIO *next = BIO_next(b); |
400 | |
|
401 | 0 | if (next == NULL) |
402 | 0 | return 0; |
403 | | |
404 | 0 | return BIO_callback_ctrl(next, cmd, fp); |
405 | 0 | } |
406 | | |
407 | | int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, |
408 | | const unsigned char *i, int e) |
409 | 0 | { |
410 | 0 | BIO_ENC_CTX *ctx; |
411 | 0 | BIO_callback_fn_ex callback_ex; |
412 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
413 | 0 | long (*callback) (struct bio_st *, int, const char *, int, long, long) = NULL; |
414 | 0 | #endif |
415 | |
|
416 | 0 | ctx = BIO_get_data(b); |
417 | 0 | if (ctx == NULL) |
418 | 0 | return 0; |
419 | | |
420 | 0 | if ((callback_ex = BIO_get_callback_ex(b)) != NULL) { |
421 | 0 | if (callback_ex(b, BIO_CB_CTRL, (const char *)c, 0, BIO_CTRL_SET, |
422 | 0 | e, 1, NULL) <= 0) |
423 | 0 | return 0; |
424 | 0 | } |
425 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
426 | 0 | else { |
427 | 0 | callback = BIO_get_callback(b); |
428 | |
|
429 | 0 | if ((callback != NULL) && |
430 | 0 | (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, |
431 | 0 | 0L) <= 0)) |
432 | 0 | return 0; |
433 | 0 | } |
434 | 0 | #endif |
435 | | |
436 | 0 | BIO_set_init(b, 1); |
437 | |
|
438 | 0 | if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e)) |
439 | 0 | return 0; |
440 | | |
441 | 0 | if (callback_ex != NULL) |
442 | 0 | return callback_ex(b, BIO_CB_CTRL | BIO_CB_RETURN, (const char *)c, 0, |
443 | 0 | BIO_CTRL_SET, e, 1, NULL); |
444 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
445 | 0 | else if (callback != NULL) |
446 | 0 | return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L); |
447 | 0 | #endif |
448 | 0 | return 1; |
449 | 0 | } |