From ab00ddb55907317d6cece552d12ddf3263c01043 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Tue, 28 Jul 2020 22:00:09 +0200 Subject: [PATCH] OSSL_PARAM: Add string pointer getters When some function receives an OSSL_PARAM array to pilfer for data, and there is a string of some sort, and all the code needs is to get the pointer to the data, rather than a copy, there is currently no other way than to use |param->data| directly. This is of course a valid method, but lacks any safety check (is |param->data_type| correct, for example?). OSSL_PARAM_get_utf8_string_ptr() and OSSL_PARAM_get_octet_string_ptr() helps the programmer with such things, by setting the argument pointer to |param->data|. Additionally, the handle the data types OSSL_PARAM_UTF8_PTR and OSSL_PARAM_OCTET_PTR as well. Reviewed-by: Shane Lontis (Merged from https://github.com/openssl/openssl/pull/12512) --- crypto/params.c | 26 ++++++++++++++++++++++++++ doc/man3/OSSL_PARAM_int.pod | 16 ++++++++++++++++ include/openssl/params.h | 4 ++++ util/libcrypto.num | 2 ++ 4 files changed, 48 insertions(+) diff --git a/crypto/params.c b/crypto/params.c index 67ca4f0c83..4f7e25e0ca 100644 --- a/crypto/params.c +++ b/crypto/params.c @@ -969,3 +969,29 @@ OSSL_PARAM OSSL_PARAM_construct_end(void) return end; } + +static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val, + size_t *used_len, unsigned int type) +{ + if (val == NULL || p == NULL || p->data_type != type) + return 0; + if (used_len != NULL) + *used_len = p->data_size; + *val = p->data; + return 1; +} + +int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val) +{ + return OSSL_PARAM_get_utf8_ptr(p, val) + || get_string_ptr_internal(p, (const void **)val, NULL, + OSSL_PARAM_UTF8_STRING); +} + +int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val, + size_t *used_len) +{ + return OSSL_PARAM_get_octet_ptr(p, val, used_len) + || get_string_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_STRING); +} + diff --git a/doc/man3/OSSL_PARAM_int.pod b/doc/man3/OSSL_PARAM_int.pod index 6712a07327..691bc3b340 100644 --- a/doc/man3/OSSL_PARAM_int.pod +++ b/doc/man3/OSSL_PARAM_int.pod @@ -24,6 +24,7 @@ OSSL_PARAM_get_time_t, OSSL_PARAM_get_uint, OSSL_PARAM_get_uint32, OSSL_PARAM_get_uint64, OSSL_PARAM_get_ulong, OSSL_PARAM_get_BN, OSSL_PARAM_get_utf8_string, OSSL_PARAM_get_octet_string, OSSL_PARAM_get_utf8_ptr, OSSL_PARAM_get_octet_ptr, +OSSL_PARAM_get_utf8_string_ptr, OSSL_PARAM_get_octet_string_ptr, OSSL_PARAM_set_double, OSSL_PARAM_set_int, OSSL_PARAM_set_int32, OSSL_PARAM_set_int64, OSSL_PARAM_set_long, OSSL_PARAM_set_size_t, OSSL_PARAM_set_time_t, OSSL_PARAM_set_uint, OSSL_PARAM_set_uint32, @@ -96,6 +97,10 @@ OSSL_PARAM_UNMODIFIED, OSSL_PARAM_modified, OSSL_PARAM_set_all_unmodified int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val, size_t used_len); + int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val); + int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val, + size_t *used_len); + int OSSL_PARAM_modified(const OSSL_PARAM *param); void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *params); @@ -264,6 +269,17 @@ OSSL_PARAM_set_octet_ptr() sets the OCTET string pointer in the parameter referenced by B

to the values B. The length of the OCTET string is provided by B. +OSSL_PARAM_get_utf8_string_ptr() retrieves the pointer to a UTF8 string from +the parameter pointed to by B

, and stores that pointer in B<*val>. +This is different from OSSL_PARAM_get_utf8_string(), which copies the +string. + +OSSL_PARAM_get_octet_string_ptr() retrieves the pointer to a octet string +from the parameter pointed to by B

, and stores that pointer in B<*val>, +along with the string's length in B<*used_len>. +This is different from OSSL_PARAM_get_octet_string(), which copies the +string. + The OSSL_PARAM_UNMODIFIED macro is used to detect if a parameter was set. On creation, via either the macros or construct calls, the I field is set to this. If the parameter is set using the calls defined herein, the diff --git a/include/openssl/params.h b/include/openssl/params.h index 44fc1a6a38..6ed7ecbb24 100644 --- a/include/openssl/params.h +++ b/include/openssl/params.h @@ -142,6 +142,10 @@ int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val, int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val, size_t used_len); +int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val); +int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val, + size_t *used_len); + int OSSL_PARAM_modified(const OSSL_PARAM *p); void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p); diff --git a/util/libcrypto.num b/util/libcrypto.num index 575731b145..5cda5b3d8d 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -5281,3 +5281,5 @@ OSSL_STORE_LOADER_number ? 3_0_0 EXIST::FUNCTION: OSSL_STORE_LOADER_is_a ? 3_0_0 EXIST::FUNCTION: OSSL_STORE_LOADER_do_all_provided ? 3_0_0 EXIST::FUNCTION: OSSL_STORE_LOADER_names_do_all ? 3_0_0 EXIST::FUNCTION: +OSSL_PARAM_get_utf8_string_ptr ? 3_0_0 EXIST::FUNCTION: +OSSL_PARAM_get_octet_string_ptr ? 3_0_0 EXIST::FUNCTION: