From 8f3b8fd6f45fc5f2ab924011908a1e66c2dba462 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Thu, 26 Sep 2019 07:45:33 +0200 Subject: [PATCH] OSSL_PARAM functions: change to allow the data field to be NULL Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/10025) --- crypto/params.c | 35 ++++++++++++++++++++++++++++++++++- doc/man3/OSSL_PARAM_int.pod | 14 +++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/crypto/params.c b/crypto/params.c index 20082ad90b..b2ceb13278 100644 --- a/crypto/params.c +++ b/crypto/params.c @@ -211,6 +211,8 @@ int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val) p->return_size = 0; if (p->data_type == OSSL_PARAM_INTEGER) { p->return_size = sizeof(int32_t); /* Minimum expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(int32_t): *(int32_t *)p->data = val; @@ -222,6 +224,8 @@ int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val) } } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) { p->return_size = sizeof(uint32_t); /* Minimum expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(uint32_t): *(uint32_t *)p->data = (uint32_t)val; @@ -233,6 +237,8 @@ int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val) } } else if (p->data_type == OSSL_PARAM_REAL) { p->return_size = sizeof(double); + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(double): *(double *)p->data = (double)val; @@ -310,6 +316,8 @@ int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val) if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { p->return_size = sizeof(uint32_t); /* Minimum expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(uint32_t): *(uint32_t *)p->data = val; @@ -321,6 +329,8 @@ int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val) } } else if (p->data_type == OSSL_PARAM_INTEGER) { p->return_size = sizeof(int32_t); /* Minimum expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(int32_t): if (val <= INT32_MAX) { @@ -335,6 +345,8 @@ int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val) } } else if (p->data_type == OSSL_PARAM_REAL) { p->return_size = sizeof(double); + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(double): *(double *)p->data = (double)val; @@ -403,6 +415,8 @@ int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val) p->return_size = 0; if (p->data_type == OSSL_PARAM_INTEGER) { p->return_size = sizeof(int64_t); /* Expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(int32_t): if (val >= INT32_MIN && val <= INT32_MAX) { @@ -417,6 +431,8 @@ int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val) } } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) { p->return_size = sizeof(uint64_t); /* Expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(uint32_t): if (val <= UINT32_MAX) { @@ -431,6 +447,8 @@ int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val) } } else if (p->data_type == OSSL_PARAM_REAL) { p->return_size = sizeof(double); + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(double): u64 = val < 0 ? -val : val; @@ -506,6 +524,8 @@ int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val) if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) { p->return_size = sizeof(uint64_t); /* Expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(uint32_t): if (val <= UINT32_MAX) { @@ -520,6 +540,8 @@ int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val) } } else if (p->data_type == OSSL_PARAM_INTEGER) { p->return_size = sizeof(int64_t); /* Expected size */ + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(int32_t): if (val <= INT32_MAX) { @@ -616,6 +638,8 @@ int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val) bytes = (size_t)BN_num_bytes(val); p->return_size = bytes; + if (p->data == NULL) + return 1; return p->data_size >= bytes && BN_bn2nativepad(val, p->data, bytes) >= 0; } @@ -680,6 +704,8 @@ int OSSL_PARAM_set_double(OSSL_PARAM *p, double val) if (p->data_type == OSSL_PARAM_REAL) { p->return_size = sizeof(double); + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(double): *(double *)p->data = val; @@ -688,6 +714,8 @@ int OSSL_PARAM_set_double(OSSL_PARAM *p, double val) } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val == (ossl_uintmax_t)val) { p->return_size = sizeof(double); + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(uint32_t): if (val >= 0 && val <= UINT32_MAX) { @@ -705,6 +733,8 @@ int OSSL_PARAM_set_double(OSSL_PARAM *p, double val) break; } } else if (p->data_type == OSSL_PARAM_INTEGER && val == (ossl_intmax_t)val) { p->return_size = sizeof(double); + if (p->data == NULL) + return 1; switch (p->data_size) { case sizeof(int32_t): if (val >= INT32_MIN && val <= INT32_MAX) { @@ -775,6 +805,8 @@ static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len, unsigned int type) { p->return_size = len; + if (p->data == NULL) + return 1; if (p->data_type != type || p->data_size < len) return 0; @@ -847,7 +879,8 @@ static int set_ptr_internal(OSSL_PARAM *p, const void *val, p->return_size = len; if (p->data_type != type) return 0; - *(const void **)p->data = val; + if (p->data != NULL) + *(const void **)p->data = val; return 1; } diff --git a/doc/man3/OSSL_PARAM_int.pod b/doc/man3/OSSL_PARAM_int.pod index 742e8d5774..991b3d1212 100644 --- a/doc/man3/OSSL_PARAM_int.pod +++ b/doc/man3/OSSL_PARAM_int.pod @@ -198,6 +198,8 @@ Type coercion takes place as discussed in the NOTES section. OSSL_PARAM_set_TYPE() stores a value B of type B into the parameter B

. +If the parameter's I field is NULL, then only its I field +will be assigned the size the parameter's I buffer should have. Type coercion takes place as discussed in the NOTES section. OSSL_PARAM_get_BN() retrieves a BIGNUM from the parameter pointed to by B

. @@ -205,6 +207,8 @@ The BIGNUM referenced by B is updated and is allocated if B<*val> is B. OSSL_PARAM_set_BN() stores the BIGNUM B into the parameter B

. +If the parameter's I field is NULL, then only its I field +will be assigned the size the parameter's I buffer should have. OSSL_PARAM_get_utf8_string() retrieves a UTF8 string from the parameter pointed to by B

. @@ -215,6 +219,8 @@ If memory is allocated by this function, it must be freed by the caller. OSSL_PARAM_set_utf8_string() sets a UTF8 string from the parameter pointed to by B

to the value referenced by B. +If the parameter's I field is NULL, then only its I field +will be assigned the size the parameter's I buffer should have. OSSL_PARAM_get_octet_string() retrieves an OCTET string from the parameter pointed to by B

. @@ -225,6 +231,8 @@ If memory is allocated by this function, it must be freed by the caller. OSSL_PARAM_set_octet_string() sets an OCTET string from the parameter pointed to by B

to the value referenced by B. +If the parameter's I field is NULL, then only its I field +will be assigned the size the parameter's I buffer should have. OSSL_PARAM_get_utf8_ptr() retrieves the UTF8 string pointer from the parameter referenced by B

and stores it in B<*val>. @@ -260,9 +268,9 @@ representable by the target type or parameter. Apart from that, the functions must be used appropriately for the expected type of the parameter. -For OSSL_PARAM_get_utf8_ptr() and OSSL_PARAM_get_octet_ptr(), B -is not relevant if the purpose is to send the B array to a -I, i.e. to get parameter data back. +For OSSL_PARAM_construct_utf8_ptr() and OSSL_PARAM_consstruct_octet_ptr(), +B is not relevant if the purpose is to send the B array +to a I, i.e. to get parameter data back. In that case, B can safely be given zero. See L for further information on the possible purposes. -- 2.34.1