prov: add a safe memdup function for context cloning

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/17572)
This commit is contained in:
Pauli 2022-01-26 15:21:51 +11:00
parent a841d450a4
commit 5b030ec080
2 changed files with 21 additions and 0 deletions

View File

@ -136,3 +136,7 @@ typedef struct ag_capable_st {
*/
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
OSSL_ALGORITHM *out);
/* Duplicate a lump of memory safely */
int ossl_prov_memdup(const void *src, size_t src_len,
unsigned char **dest, size_t *dest_len);

View File

@ -351,3 +351,20 @@ void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
out[j++] = in[i].alg;
}
}
/* Duplicate a lump of memory safely */
int ossl_prov_memdup(const void *src, size_t src_len,
unsigned char **dest, size_t *dest_len)
{
if (src != NULL) {
if ((*dest = OPENSSL_memdup(src, src_len)) == NULL) {
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
return 0;
}
*dest_len = src_len;
} else {
*dest = NULL;
*dest_len = 0;
}
return 1;
}