]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
params: add helper functions that don't locate the parameters
authorPauli <ppzgs1@gmail.com>
Mon, 30 Jun 2025 03:09:29 +0000 (13:09 +1000)
committerTomas Mraz <tomas@openssl.org>
Thu, 31 Jul 2025 18:20:48 +0000 (20:20 +0200)
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/27923)

crypto/params.c
include/internal/params.h

index 0c7dcadb099554708676b76aecc77e8cde1f4ec4..127fae205aa27e60d53b74c7584216b4b08eef66 100644 (file)
@@ -1562,10 +1562,10 @@ OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
  * *out and *out_len are guaranteed to be untouched if this function
  * doesn't return success.
  */
-int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
-                                 unsigned char **out, size_t *out_len)
+int ossl_param_get1_octet_string_from_param(const OSSL_PARAM *p,
+                                            unsigned char **out,
+                                            size_t *out_len)
 {
-    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
     void *buf = NULL;
     size_t len = 0;
 
@@ -1583,11 +1583,20 @@ int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
     return 1;
 }
 
-static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
+int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
+                                 unsigned char **out, size_t *out_len)
+{
+    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
+
+    return ossl_param_get1_octet_string_from_param(p, out, out_len);
+}
+
+static int setbuf_fromparams(size_t n, OSSL_PARAM *p[],
                              unsigned char *out, size_t *outlen)
 {
     int ret = 0;
     WPACKET pkt;
+    size_t i;
 
     if (out == NULL) {
         if (!WPACKET_init_null(&pkt, 0))
@@ -1597,12 +1606,12 @@ static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
             return 0;
     }
 
-    for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) {
-        if (p->data_type != OSSL_PARAM_OCTET_STRING)
+    for (i = 0; i < n; i++) {
+        if (p[i]->data_type != OSSL_PARAM_OCTET_STRING)
             goto err;
-        if (p->data != NULL
-                && p->data_size != 0
-                && !WPACKET_memcpy(&pkt, p->data, p->data_size))
+        if (p[i]->data != NULL
+                && p[i]->data_size != 0
+                && !WPACKET_memcpy(&pkt, p[i]->data, p[i]->data_size))
             goto err;
     }
     if (!WPACKET_get_total_written(&pkt, outlen)
@@ -1614,19 +1623,18 @@ err:
     return ret;
 }
 
-int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *name,
+int ossl_param_get1_concat_octet_string(size_t n, OSSL_PARAM *params[],
                                         unsigned char **out,
                                         size_t *out_len, size_t maxsize)
 {
-    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
     unsigned char *res;
     size_t sz = 0;
 
-    if (p == NULL)
-        return -1;
+    if (n == 0)
+        return 1;
 
     /* Calculate the total size */
-    if (!setbuf_fromparams(p, name, NULL, &sz))
+    if (!setbuf_fromparams(n, params, NULL, &sz))
         return 0;
 
     /* Check that it's not oversized */
@@ -1646,7 +1654,7 @@ int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *na
         return 0;
 
     /* Concat one or more OSSL_KDF_PARAM_INFO fields */
-    if (!setbuf_fromparams(p, name, res, &sz)) {
+    if (!setbuf_fromparams(n, params, res, &sz)) {
         OPENSSL_clear_free(res, sz);
         return 0;
     }
index 3fbd0cf954c53d0d833737e5c418a20860da2ad0..c2a272ee9e0db1d23eceba29f5a158ee6a64c0ad 100644 (file)
  * *out and *out_len are guaranteed to be untouched if this function
  * doesn't return success.
  */
+int ossl_param_get1_octet_string_from_param(const OSSL_PARAM *p,
+                                            unsigned char **out,
+                                            size_t *out_len);
 int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
                                  unsigned char **out, size_t *out_len);
+
 /*
  * Concatenate all of the matching params together.
  * *out will point to an allocated buffer on successful return.
@@ -28,11 +32,11 @@ int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
  *
  * Passing 0 for maxsize means unlimited size output.
  *
- * Returns 1 on success, 0 on failure and -1 if there are no matching params.
+ * Returns 1 on success and 0 on failure.
  *
  * *out and *out_len are guaranteed to be untouched if this function
  * doesn't return success.
  */
-int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *name,
-                                        unsigned char **out, size_t *out_len,
-                                        size_t maxsize);
+int ossl_param_get1_concat_octet_string(size_t n, OSSL_PARAM *params[],
+                                        unsigned char **out,
+                                        size_t *out_len, size_t maxsize);