]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
params: refactor some of the param helper code
authorPauli <ppzgs1@gmail.com>
Mon, 31 Mar 2025 22:57:50 +0000 (09:57 +1100)
committerMatt Caswell <matt@openssl.org>
Fri, 18 Apr 2025 14:23:42 +0000 (15:23 +0100)
Unifies some duplicated code.

Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/27221)

crypto/params.c

index 7fb116c63642687db2e465d11aaadf2fa18d6f1d..01aff0739b1a1fad74449bf7d553903e77119609 100644 (file)
@@ -1471,6 +1471,15 @@ OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
     return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
 }
 
+static int get_ptr_internal_skip_checks(const OSSL_PARAM *p, const void **val,
+                                        size_t *used_len)
+{
+    if (used_len != NULL)
+        *used_len = p->data_size;
+    *val = *(const void **)p->data;
+    return 1;
+}
+
 static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
                             size_t *used_len, unsigned int type)
 {
@@ -1482,10 +1491,7 @@ static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
         err_bad_type;
         return 0;
     }
-    if (used_len != NULL)
-        *used_len = p->data_size;
-    *val = *(const void **)p->data;
-    return 1;
+    return get_ptr_internal_skip_checks(p, val, used_len);
 }
 
 int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
@@ -1659,12 +1665,17 @@ OSSL_PARAM OSSL_PARAM_construct_end(void)
 }
 
 static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
-                                   size_t *used_len, unsigned int type)
+                                   size_t *used_len, unsigned int ref_type,
+                                   unsigned int type)
 {
     if (val == NULL || p == NULL) {
         err_null_argument;
         return 0;
     }
+
+    if (p->data_type == ref_type)
+        return get_ptr_internal_skip_checks(p, (const void **)val, used_len);
+
     if (p->data_type != type) {
         err_bad_type;
         return 0;
@@ -1677,27 +1688,17 @@ static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
 
 int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
 {
-    int rv;
-
-    ERR_set_mark();
-    rv = OSSL_PARAM_get_utf8_ptr(p, val);
-    ERR_pop_to_mark();
-
-    return rv || get_string_ptr_internal(p, (const void **)val, NULL,
-                                         OSSL_PARAM_UTF8_STRING);
+    return get_string_ptr_internal(p, (const void **)val, NULL,
+                                   OSSL_PARAM_UTF8_PTR,
+                                   OSSL_PARAM_UTF8_STRING);
 }
 
 int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
                                     size_t *used_len)
 {
-    int rv;
-
-    ERR_set_mark();
-    rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
-    ERR_pop_to_mark();
-
-    return rv || get_string_ptr_internal(p, val, used_len,
-                                         OSSL_PARAM_OCTET_STRING);
+    return get_string_ptr_internal(p, (const void **)val, used_len,
+                                   OSSL_PARAM_OCTET_PTR,
+                                   OSSL_PARAM_OCTET_STRING);
 }
 
 int OSSL_PARAM_set_octet_string_or_ptr(OSSL_PARAM *p, const void *val,