diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/evp/bio_enc.c | 131 |
1 files changed, 67 insertions, 64 deletions
diff --git a/src/lib/libcrypto/evp/bio_enc.c b/src/lib/libcrypto/evp/bio_enc.c index 62712830a8..30baf93517 100644 --- a/src/lib/libcrypto/evp/bio_enc.c +++ b/src/lib/libcrypto/evp/bio_enc.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bio_enc.c,v 1.32 2024/04/09 13:52:41 beck Exp $ */ | 1 | /* $OpenBSD: bio_enc.c,v 1.33 2024/04/12 11:10:34 tb Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -105,7 +105,7 @@ static const BIO_METHOD methods_enc = { | |||
| 105 | const BIO_METHOD * | 105 | const BIO_METHOD * |
| 106 | BIO_f_cipher(void) | 106 | BIO_f_cipher(void) |
| 107 | { | 107 | { |
| 108 | return (&methods_enc); | 108 | return &methods_enc; |
| 109 | } | 109 | } |
| 110 | LCRYPTO_ALIAS(BIO_f_cipher); | 110 | LCRYPTO_ALIAS(BIO_f_cipher); |
| 111 | 111 | ||
| @@ -120,7 +120,7 @@ bio_enc_ctx_free(BIO_ENC_CTX *ctx) | |||
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | static int | 122 | static int |
| 123 | enc_new(BIO *bi) | 123 | enc_new(BIO *bio) |
| 124 | { | 124 | { |
| 125 | BIO_ENC_CTX *ctx; | 125 | BIO_ENC_CTX *ctx; |
| 126 | int ret = 0; | 126 | int ret = 0; |
| @@ -133,7 +133,7 @@ enc_new(BIO *bi) | |||
| 133 | ctx->cont = 1; | 133 | ctx->cont = 1; |
| 134 | ctx->ok = 1; | 134 | ctx->ok = 1; |
| 135 | 135 | ||
| 136 | bi->ptr = ctx; | 136 | bio->ptr = ctx; |
| 137 | ctx = NULL; | 137 | ctx = NULL; |
| 138 | 138 | ||
| 139 | ret = 1; | 139 | ret = 1; |
| @@ -145,29 +145,29 @@ enc_new(BIO *bi) | |||
| 145 | } | 145 | } |
| 146 | 146 | ||
| 147 | static int | 147 | static int |
| 148 | enc_free(BIO *a) | 148 | enc_free(BIO *bio) |
| 149 | { | 149 | { |
| 150 | if (a == NULL) | 150 | if (bio == NULL) |
| 151 | return 0; | 151 | return 0; |
| 152 | 152 | ||
| 153 | bio_enc_ctx_free(a->ptr); | 153 | bio_enc_ctx_free(bio->ptr); |
| 154 | explicit_bzero(a, sizeof(*a)); | 154 | explicit_bzero(bio, sizeof(*bio)); |
| 155 | 155 | ||
| 156 | return 1; | 156 | return 1; |
| 157 | } | 157 | } |
| 158 | 158 | ||
| 159 | static int | 159 | static int |
| 160 | enc_read(BIO *b, char *out, int outl) | 160 | enc_read(BIO *bio, char *out, int outl) |
| 161 | { | 161 | { |
| 162 | int ret = 0, i; | ||
| 163 | BIO_ENC_CTX *ctx; | 162 | BIO_ENC_CTX *ctx; |
| 163 | int ret = 0, i; | ||
| 164 | 164 | ||
| 165 | if (out == NULL) | 165 | if (out == NULL) |
| 166 | return (0); | 166 | return 0; |
| 167 | ctx = (BIO_ENC_CTX *)b->ptr; | 167 | ctx = bio->ptr; |
| 168 | 168 | ||
| 169 | if ((ctx == NULL) || (b->next_bio == NULL)) | 169 | if (ctx == NULL || bio->next_bio == NULL) |
| 170 | return (0); | 170 | return 0; |
| 171 | 171 | ||
| 172 | /* First check if there are bytes decoded/encoded */ | 172 | /* First check if there are bytes decoded/encoded */ |
| 173 | if (ctx->buf_len > 0) { | 173 | if (ctx->buf_len > 0) { |
| @@ -194,11 +194,12 @@ enc_read(BIO *b, char *out, int outl) | |||
| 194 | 194 | ||
| 195 | /* read in at IV offset, read the EVP_Cipher | 195 | /* read in at IV offset, read the EVP_Cipher |
| 196 | * documentation about why */ | 196 | * documentation about why */ |
| 197 | i = BIO_read(b->next_bio, &(ctx->buf[BUF_OFFSET]), ENC_BLOCK_SIZE); | 197 | i = BIO_read(bio->next_bio, &ctx->buf[BUF_OFFSET], |
| 198 | ENC_BLOCK_SIZE); | ||
| 198 | 199 | ||
| 199 | if (i <= 0) { | 200 | if (i <= 0) { |
| 200 | /* Should be continue next time we are called? */ | 201 | /* Should be continue next time we are called? */ |
| 201 | if (!BIO_should_retry(b->next_bio)) { | 202 | if (!BIO_should_retry(bio->next_bio)) { |
| 202 | ctx->cont = i; | 203 | ctx->cont = i; |
| 203 | i = EVP_CipherFinal_ex(ctx->cipher_ctx, | 204 | i = EVP_CipherFinal_ex(ctx->cipher_ctx, |
| 204 | (unsigned char *)ctx->buf, | 205 | (unsigned char *)ctx->buf, |
| @@ -212,7 +213,7 @@ enc_read(BIO *b, char *out, int outl) | |||
| 212 | } else { | 213 | } else { |
| 213 | EVP_CipherUpdate(ctx->cipher_ctx, | 214 | EVP_CipherUpdate(ctx->cipher_ctx, |
| 214 | (unsigned char *)ctx->buf, &ctx->buf_len, | 215 | (unsigned char *)ctx->buf, &ctx->buf_len, |
| 215 | (unsigned char *)&(ctx->buf[BUF_OFFSET]), i); | 216 | (unsigned char *)&ctx->buf[BUF_OFFSET], i); |
| 216 | ctx->cont = 1; | 217 | ctx->cont = 1; |
| 217 | /* Note: it is possible for EVP_CipherUpdate to | 218 | /* Note: it is possible for EVP_CipherUpdate to |
| 218 | * decrypt zero bytes because this is or looks like | 219 | * decrypt zero bytes because this is or looks like |
| @@ -237,39 +238,39 @@ enc_read(BIO *b, char *out, int outl) | |||
| 237 | out += i; | 238 | out += i; |
| 238 | } | 239 | } |
| 239 | 240 | ||
| 240 | BIO_clear_retry_flags(b); | 241 | BIO_clear_retry_flags(bio); |
| 241 | BIO_copy_next_retry(b); | 242 | BIO_copy_next_retry(bio); |
| 242 | return ((ret == 0) ? ctx->cont : ret); | 243 | return ret == 0 ? ctx->cont : ret; |
| 243 | } | 244 | } |
| 244 | 245 | ||
| 245 | static int | 246 | static int |
| 246 | enc_write(BIO *b, const char *in, int inl) | 247 | enc_write(BIO *bio, const char *in, int inl) |
| 247 | { | 248 | { |
| 248 | int ret = 0, n, i; | ||
| 249 | BIO_ENC_CTX *ctx; | 249 | BIO_ENC_CTX *ctx; |
| 250 | int ret = 0, n, i; | ||
| 250 | 251 | ||
| 251 | ctx = (BIO_ENC_CTX *)b->ptr; | 252 | ctx = bio->ptr; |
| 252 | ret = inl; | 253 | ret = inl; |
| 253 | 254 | ||
| 254 | BIO_clear_retry_flags(b); | 255 | BIO_clear_retry_flags(bio); |
| 255 | n = ctx->buf_len - ctx->buf_off; | 256 | n = ctx->buf_len - ctx->buf_off; |
| 256 | while (n > 0) { | 257 | while (n > 0) { |
| 257 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); | 258 | i = BIO_write(bio->next_bio, &(ctx->buf[ctx->buf_off]), n); |
| 258 | if (i <= 0) { | 259 | if (i <= 0) { |
| 259 | BIO_copy_next_retry(b); | 260 | BIO_copy_next_retry(bio); |
| 260 | return (i); | 261 | return i; |
| 261 | } | 262 | } |
| 262 | ctx->buf_off += i; | 263 | ctx->buf_off += i; |
| 263 | n -= i; | 264 | n -= i; |
| 264 | } | 265 | } |
| 265 | /* at this point all pending data has been written */ | 266 | /* at this point all pending data has been written */ |
| 266 | 267 | ||
| 267 | if ((in == NULL) || (inl <= 0)) | 268 | if (in == NULL || inl <= 0) |
| 268 | return (0); | 269 | return 0; |
| 269 | 270 | ||
| 270 | ctx->buf_off = 0; | 271 | ctx->buf_off = 0; |
| 271 | while (inl > 0) { | 272 | while (inl > 0) { |
| 272 | n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl; | 273 | n = inl > ENC_BLOCK_SIZE ? ENC_BLOCK_SIZE : inl; |
| 273 | EVP_CipherUpdate(ctx->cipher_ctx, | 274 | EVP_CipherUpdate(ctx->cipher_ctx, |
| 274 | (unsigned char *)ctx->buf, &ctx->buf_len, | 275 | (unsigned char *)ctx->buf, &ctx->buf_len, |
| 275 | (unsigned char *)in, n); | 276 | (unsigned char *)in, n); |
| @@ -279,10 +280,10 @@ enc_write(BIO *b, const char *in, int inl) | |||
| 279 | ctx->buf_off = 0; | 280 | ctx->buf_off = 0; |
| 280 | n = ctx->buf_len; | 281 | n = ctx->buf_len; |
| 281 | while (n > 0) { | 282 | while (n > 0) { |
| 282 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); | 283 | i = BIO_write(bio->next_bio, &ctx->buf[ctx->buf_off], n); |
| 283 | if (i <= 0) { | 284 | if (i <= 0) { |
| 284 | BIO_copy_next_retry(b); | 285 | BIO_copy_next_retry(bio); |
| 285 | return (ret == inl) ? i : ret - inl; | 286 | return ret == inl ? i : ret - inl; |
| 286 | } | 287 | } |
| 287 | n -= i; | 288 | n -= i; |
| 288 | ctx->buf_off += i; | 289 | ctx->buf_off += i; |
| @@ -290,20 +291,21 @@ enc_write(BIO *b, const char *in, int inl) | |||
| 290 | ctx->buf_len = 0; | 291 | ctx->buf_len = 0; |
| 291 | ctx->buf_off = 0; | 292 | ctx->buf_off = 0; |
| 292 | } | 293 | } |
| 293 | BIO_copy_next_retry(b); | 294 | BIO_copy_next_retry(bio); |
| 294 | return (ret); | 295 | |
| 296 | return ret; | ||
| 295 | } | 297 | } |
| 296 | 298 | ||
| 297 | static long | 299 | static long |
| 298 | enc_ctrl(BIO *b, int cmd, long num, void *ptr) | 300 | enc_ctrl(BIO *bio, int cmd, long num, void *ptr) |
| 299 | { | 301 | { |
| 300 | BIO *dbio; | 302 | BIO *dbio; |
| 301 | BIO_ENC_CTX *ctx, *dctx; | 303 | BIO_ENC_CTX *ctx, *dctx; |
| 302 | long ret = 1; | ||
| 303 | int i; | ||
| 304 | EVP_CIPHER_CTX **c_ctx; | 304 | EVP_CIPHER_CTX **c_ctx; |
| 305 | int i; | ||
| 306 | long ret = 1; | ||
| 305 | 307 | ||
| 306 | ctx = b->ptr; | 308 | ctx = bio->ptr; |
| 307 | 309 | ||
| 308 | switch (cmd) { | 310 | switch (cmd) { |
| 309 | case BIO_CTRL_RESET: | 311 | case BIO_CTRL_RESET: |
| @@ -311,29 +313,29 @@ enc_ctrl(BIO *b, int cmd, long num, void *ptr) | |||
| 311 | ctx->finished = 0; | 313 | ctx->finished = 0; |
| 312 | EVP_CipherInit_ex(ctx->cipher_ctx, NULL, NULL, NULL, NULL, | 314 | EVP_CipherInit_ex(ctx->cipher_ctx, NULL, NULL, NULL, NULL, |
| 313 | ctx->cipher_ctx->encrypt); | 315 | ctx->cipher_ctx->encrypt); |
| 314 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 316 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 315 | break; | 317 | break; |
| 316 | case BIO_CTRL_EOF: /* More to read */ | 318 | case BIO_CTRL_EOF: /* More to read */ |
| 317 | if (ctx->cont <= 0) | 319 | if (ctx->cont <= 0) |
| 318 | ret = 1; | 320 | ret = 1; |
| 319 | else | 321 | else |
| 320 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 322 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 321 | break; | 323 | break; |
| 322 | case BIO_CTRL_WPENDING: | 324 | case BIO_CTRL_WPENDING: |
| 323 | ret = ctx->buf_len - ctx->buf_off; | 325 | ret = ctx->buf_len - ctx->buf_off; |
| 324 | if (ret <= 0) | 326 | if (ret <= 0) |
| 325 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 327 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 326 | break; | 328 | break; |
| 327 | case BIO_CTRL_PENDING: /* More to read in buffer */ | 329 | case BIO_CTRL_PENDING: /* More to read in buffer */ |
| 328 | ret = ctx->buf_len - ctx->buf_off; | 330 | ret = ctx->buf_len - ctx->buf_off; |
| 329 | if (ret <= 0) | 331 | if (ret <= 0) |
| 330 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 332 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 331 | break; | 333 | break; |
| 332 | case BIO_CTRL_FLUSH: | 334 | case BIO_CTRL_FLUSH: |
| 333 | /* do a final write */ | 335 | /* do a final write */ |
| 334 | again: | 336 | again: |
| 335 | while (ctx->buf_len != ctx->buf_off) { | 337 | while (ctx->buf_len != ctx->buf_off) { |
| 336 | i = enc_write(b, NULL, 0); | 338 | i = enc_write(bio, NULL, 0); |
| 337 | if (i < 0) | 339 | if (i < 0) |
| 338 | return i; | 340 | return i; |
| 339 | } | 341 | } |
| @@ -343,7 +345,7 @@ enc_ctrl(BIO *b, int cmd, long num, void *ptr) | |||
| 343 | ctx->buf_off = 0; | 345 | ctx->buf_off = 0; |
| 344 | ret = EVP_CipherFinal_ex(ctx->cipher_ctx, | 346 | ret = EVP_CipherFinal_ex(ctx->cipher_ctx, |
| 345 | (unsigned char *)ctx->buf, | 347 | (unsigned char *)ctx->buf, |
| 346 | &(ctx->buf_len)); | 348 | &ctx->buf_len); |
| 347 | ctx->ok = (int)ret; | 349 | ctx->ok = (int)ret; |
| 348 | if (ret <= 0) | 350 | if (ret <= 0) |
| 349 | break; | 351 | break; |
| @@ -353,20 +355,20 @@ enc_ctrl(BIO *b, int cmd, long num, void *ptr) | |||
| 353 | } | 355 | } |
| 354 | 356 | ||
| 355 | /* Finally flush the underlying BIO */ | 357 | /* Finally flush the underlying BIO */ |
| 356 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 358 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 357 | break; | 359 | break; |
| 358 | case BIO_C_GET_CIPHER_STATUS: | 360 | case BIO_C_GET_CIPHER_STATUS: |
| 359 | ret = (long)ctx->ok; | 361 | ret = (long)ctx->ok; |
| 360 | break; | 362 | break; |
| 361 | case BIO_C_DO_STATE_MACHINE: | 363 | case BIO_C_DO_STATE_MACHINE: |
| 362 | BIO_clear_retry_flags(b); | 364 | BIO_clear_retry_flags(bio); |
| 363 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 365 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 364 | BIO_copy_next_retry(b); | 366 | BIO_copy_next_retry(bio); |
| 365 | break; | 367 | break; |
| 366 | case BIO_C_GET_CIPHER_CTX: | 368 | case BIO_C_GET_CIPHER_CTX: |
| 367 | c_ctx = ptr; | 369 | c_ctx = ptr; |
| 368 | *c_ctx = ctx->cipher_ctx; | 370 | *c_ctx = ctx->cipher_ctx; |
| 369 | b->init = 1; | 371 | bio->init = 1; |
| 370 | break; | 372 | break; |
| 371 | case BIO_CTRL_DUP: | 373 | case BIO_CTRL_DUP: |
| 372 | dbio = ptr; | 374 | dbio = ptr; |
| @@ -376,7 +378,7 @@ enc_ctrl(BIO *b, int cmd, long num, void *ptr) | |||
| 376 | dbio->init = 1; | 378 | dbio->init = 1; |
| 377 | break; | 379 | break; |
| 378 | default: | 380 | default: |
| 379 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | 381 | ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 380 | break; | 382 | break; |
| 381 | } | 383 | } |
| 382 | 384 | ||
| @@ -384,46 +386,47 @@ enc_ctrl(BIO *b, int cmd, long num, void *ptr) | |||
| 384 | } | 386 | } |
| 385 | 387 | ||
| 386 | static long | 388 | static long |
| 387 | enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) | 389 | enc_callback_ctrl(BIO *bio, int cmd, BIO_info_cb *fp) |
| 388 | { | 390 | { |
| 389 | long ret = 1; | 391 | long ret = 1; |
| 390 | 392 | ||
| 391 | if (b->next_bio == NULL) | 393 | if (bio->next_bio == NULL) |
| 392 | return (0); | 394 | return 0; |
| 395 | |||
| 393 | switch (cmd) { | 396 | switch (cmd) { |
| 394 | default: | 397 | default: |
| 395 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); | 398 | ret = BIO_callback_ctrl(bio->next_bio, cmd, fp); |
| 396 | break; | 399 | break; |
| 397 | } | 400 | } |
| 398 | return (ret); | 401 | |
| 402 | return ret; | ||
| 399 | } | 403 | } |
| 400 | 404 | ||
| 401 | int | 405 | int |
| 402 | BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, | 406 | BIO_set_cipher(BIO *bio, const EVP_CIPHER *c, const unsigned char *k, |
| 403 | const unsigned char *i, int e) | 407 | const unsigned char *i, int e) |
| 404 | { | 408 | { |
| 405 | BIO_ENC_CTX *ctx; | 409 | BIO_ENC_CTX *ctx; |
| 406 | long (*cb)(BIO *, int, const char *, int, long, long); | 410 | long (*cb)(BIO *, int, const char *, int, long, long); |
| 407 | 411 | ||
| 408 | if (b == NULL) | 412 | if (bio == NULL) |
| 409 | return 0; | 413 | return 0; |
| 410 | 414 | ||
| 411 | if ((ctx = BIO_get_data(b)) == NULL) | 415 | if ((ctx = BIO_get_data(bio)) == NULL) |
| 412 | return 0; | 416 | return 0; |
| 413 | 417 | ||
| 414 | if ((cb = BIO_get_callback(b)) != NULL) { | 418 | if ((cb = BIO_get_callback(bio)) != NULL) { |
| 415 | if (cb(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) | 419 | if (cb(bio, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= 0) |
| 416 | <= 0) | ||
| 417 | return 0; | 420 | return 0; |
| 418 | } | 421 | } |
| 419 | 422 | ||
| 420 | BIO_set_init(b, 1); | 423 | BIO_set_init(bio, 1); |
| 421 | 424 | ||
| 422 | if (!EVP_CipherInit_ex(ctx->cipher_ctx, c, NULL, k, i, e)) | 425 | if (!EVP_CipherInit_ex(ctx->cipher_ctx, c, NULL, k, i, e)) |
| 423 | return 0; | 426 | return 0; |
| 424 | 427 | ||
| 425 | if (cb != NULL) | 428 | if (cb != NULL) |
| 426 | return cb(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L); | 429 | return cb(bio, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L); |
| 427 | 430 | ||
| 428 | return 1; | 431 | return 1; |
| 429 | } | 432 | } |
