diff options
author | Pauli <paul.dale@oracle.com> | 2020-03-27 01:33:46 +0100 |
---|---|---|
committer | Pauli <paul.dale@oracle.com> | 2020-03-28 03:27:22 +0100 |
commit | 20c98cd45399423f760dbd75d8912769c6b7b10e (patch) | |
tree | 6d5005b1925d47baf7343811e6101988a9772b00 /crypto/param_build.c | |
parent | Param build: make structures opaque. (diff) | |
download | openssl-20c98cd45399423f760dbd75d8912769c6b7b10e.tar.xz openssl-20c98cd45399423f760dbd75d8912769c6b7b10e.zip |
Param builder: Remove the static size limit.
Prior to this, the param builder had a statically sized array internally.
This changes it so that it uses a stack instead.
Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/11390)
Diffstat (limited to 'crypto/param_build.c')
-rw-r--r-- | crypto/param_build.c | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/crypto/param_build.c b/crypto/param_build.c index 11986d999b..4f999678cb 100644 --- a/crypto/param_build.c +++ b/crypto/param_build.c @@ -13,15 +13,11 @@ #include <openssl/cryptoerr.h> #include <openssl/params.h> #include <openssl/types.h> +#include <openssl/safestack.h> #include "internal/cryptlib.h" #include "openssl/param_build.h" /* - * The number of OSSL_PARAM elements a builder will allow. - */ -#define OSSL_PARAM_BLD_MAX 25 - -/* * Special internal param type to indicate the end of an allocate OSSL_PARAM * array. */ @@ -46,11 +42,12 @@ typedef struct { } num; } OSSL_PARAM_BLD_DEF; +DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF) + struct ossl_param_bld_st { - size_t curr; size_t total_blocks; size_t secure_blocks; - OSSL_PARAM_BLD_DEF params[OSSL_PARAM_BLD_MAX]; + STACK_OF(OSSL_PARAM_BLD_DEF) *params; }; typedef union { @@ -68,14 +65,12 @@ static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key, int size, size_t alloc, int type, int secure) { - OSSL_PARAM_BLD_DEF *pd; + OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd)); - if (bld->curr >= OSSL_PARAM_BLD_MAX) { - CRYPTOerr(CRYPTO_F_PARAM_PUSH, CRYPTO_R_TOO_MANY_RECORDS); + if (pd == NULL) { + CRYPTOerr(CRYPTO_F_PARAM_PUSH, ERR_R_MALLOC_FAILURE); return NULL; } - pd = bld->params + bld->curr++; - memset(pd, 0, sizeof(*pd)); pd->key = key; pd->type = type; pd->size = size; @@ -84,6 +79,10 @@ static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key, bld->secure_blocks += pd->alloc_blocks; else bld->total_blocks += pd->alloc_blocks; + if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) { + OPENSSL_free(pd); + pd = NULL; + } return pd; } @@ -104,11 +103,30 @@ static int param_push_num(OSSL_PARAM_BLD *bld, const char *key, OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void) { - return OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD)); + OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD)); + + if (r != NULL) { + r->params = sk_OSSL_PARAM_BLD_DEF_new_null(); + if (r->params == NULL) { + OPENSSL_free(r); + r = NULL; + } + } + return r; +} + +static void free_all_params(OSSL_PARAM_BLD *bld) +{ + int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params); + + for (i = 0; i < n; i++) + OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params)); } void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld) { + free_all_params(bld); + sk_OSSL_PARAM_BLD_DEF_free(bld->params); OPENSSL_free(bld); } @@ -285,12 +303,12 @@ static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param, OSSL_PARAM_BLD_BLOCK *blk, OSSL_PARAM_BLD_BLOCK *secure) { - size_t i; + int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); OSSL_PARAM_BLD_DEF *pd; void *p; - for (i = 0; i < bld->curr; i++) { - pd = bld->params + i; + for (i = 0; i < num; i++) { + pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i); param[i].key = pd->key; param[i].data_type = pd->type; param[i].data_size = pd->size; @@ -333,7 +351,8 @@ OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld) { OSSL_PARAM_BLD_BLOCK *blk, *s = NULL; OSSL_PARAM *params, *last; - const size_t p_blks = bytes_to_blocks((1 + bld->curr) * sizeof(*params)); + const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); + const size_t p_blks = bytes_to_blocks((1 + num) * sizeof(*params)); const size_t total = ALIGN_SIZE * (p_blks + bld->total_blocks); const size_t ss = ALIGN_SIZE * bld->secure_blocks; @@ -359,8 +378,10 @@ OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld) last->data = s; last->data_type = OSSL_PARAM_ALLOCATED_END; - /* Reset for reuse */ - memset(bld, 0, sizeof(*bld)); + /* Reset builder for reuse */ + bld->total_blocks = 0; + bld->secure_blocks = 0; + free_all_params(bld); return params; } |