From: Pauli Date: Mon, 30 Jun 2025 03:09:29 +0000 (+1000) Subject: params: add helper functions that don't locate the parameters X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b5828dbbf2775fd04f112d95b92e3f78caa8d1a6;p=thirdparty%2Fopenssl.git params: add helper functions that don't locate the parameters Reviewed-by: Shane Lontis Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/27923) --- diff --git a/crypto/params.c b/crypto/params.c index 0c7dcadb099..127fae205aa 100644 --- a/crypto/params.c +++ b/crypto/params.c @@ -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; } diff --git a/include/internal/params.h b/include/internal/params.h index 3fbd0cf954c..c2a272ee9e0 100644 --- a/include/internal/params.h +++ b/include/internal/params.h @@ -19,8 +19,12 @@ * *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);