diff options
Diffstat (limited to 'providers/implementations/ciphers/cipher_rc2.c')
-rw-r--r-- | providers/implementations/ciphers/cipher_rc2.c | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/providers/implementations/ciphers/cipher_rc2.c b/providers/implementations/ciphers/cipher_rc2.c index 106f47e8667a..c535bd7e9724 100644 --- a/providers/implementations/ciphers/cipher_rc2.c +++ b/providers/implementations/ciphers/cipher_rc2.c @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -50,10 +50,8 @@ static void *rc2_dupctx(void *ctx) return NULL; ret = OPENSSL_malloc(sizeof(*ret)); - if (ret == NULL) { - ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); + if (ret == NULL) return NULL; - } *ret = *in; return ret; @@ -108,7 +106,7 @@ static int rc2_dinit(void *ctx, const unsigned char *key, size_t keylen, static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[]) { PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx; - OSSL_PARAM *p; + OSSL_PARAM *p, *p1, *p2; if (!ossl_cipher_generic_get_ctx_params(vctx, params)) return 0; @@ -117,20 +115,24 @@ static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[]) ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); return 0; } - p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS); - if (p != NULL) { + p1 = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS); + p2 = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD); + if (p1 != NULL || p2 != NULL) { long num; int i; ASN1_TYPE *type; - unsigned char *d = p->data; - unsigned char **dd = d == NULL ? NULL : &d; + unsigned char *d1 = (p1 == NULL) ? NULL : p1->data; + unsigned char *d2 = (p2 == NULL) ? NULL : p2->data; + unsigned char **dd1 = d1 == NULL ? NULL : &d1; + unsigned char **dd2 = d2 == NULL ? NULL : &d2; - if (p->data_type != OSSL_PARAM_OCTET_STRING) { + if ((p1 != NULL && p1->data_type != OSSL_PARAM_OCTET_STRING) + || (p2 != NULL && p2->data_type != OSSL_PARAM_OCTET_STRING)) { ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); return 0; } if ((type = ASN1_TYPE_new()) == NULL) { - ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); + ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); return 0; } @@ -139,16 +141,26 @@ static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[]) if (!ASN1_TYPE_set_int_octetstring(type, num, ctx->base.iv, ctx->base.ivlen)) { ASN1_TYPE_free(type); - ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); + ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); return 0; } + /* * IF the caller has a buffer, we pray to the gods they got the * size right. There's no way to tell the i2d functions... */ - i = i2d_ASN1_TYPE(type, dd); - if (i >= 0) - p->return_size = (size_t)i; + i = i2d_ASN1_TYPE(type, dd1); + if (p1 != NULL && i >= 0) + p1->return_size = (size_t)i; + + /* + * If the buffers differ, redo the i2d on the second buffer. + * Otherwise, just use |i| as computed above + */ + if (d1 != d2) + i = i2d_ASN1_TYPE(type, dd2); + if (p2 != NULL && i >= 0) + p2->return_size = (size_t)i; ASN1_TYPE_free(type); if (i < 0) { @@ -164,7 +176,7 @@ static int rc2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx; const OSSL_PARAM *p; - if (params == NULL) + if (ossl_param_is_empty(params)) return 1; if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params)) @@ -228,7 +240,7 @@ static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ flags, kbits, blkbits, ivbits); \ } \ static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \ -static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \ +static void *alg##_##kbits##_##lcmode##_newctx(void *provctx) \ { \ PROV_##UCALG##_CTX *ctx; \ if (!ossl_prov_is_running()) \ @@ -265,7 +277,7 @@ const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \ (void (*)(void))rc2_set_ctx_params }, \ { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ (void (*)(void))rc2_settable_ctx_params }, \ - { 0, NULL } \ + OSSL_DISPATCH_END \ }; /* ossl_rc2128ecb_functions */ |