mirror of
https://github.com/openssl/openssl.git
synced 2024-12-27 06:21:43 +08:00
e077455e9e
Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and at least handle the file name and line number they are called from, there's no need to report ERR_R_MALLOC_FAILURE where they are called directly, or when SSLfatal() and RLAYERfatal() is used, the reason `ERR_R_MALLOC_FAILURE` is changed to `ERR_R_CRYPTO_LIB`. There were a number of places where `ERR_R_MALLOC_FAILURE` was reported even though it was a function from a different sub-system that was called. Those places are changed to report ERR_R_{lib}_LIB, where {lib} is the name of that sub-system. Some of them are tricky to get right, as we have a lot of functions that belong in the ASN1 sub-system, and all the `sk_` calls or from the CRYPTO sub-system. Some extra adaptation was necessary where there were custom OPENSSL_malloc() wrappers, and some bugs are fixed alongside these changes. Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Hugo Landau <hlandau@openssl.org> (Merged from https://github.com/openssl/openssl/pull/19301)
162 lines
3.9 KiB
C
162 lines
3.9 KiB
C
/*
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
|
*
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
* in the file LICENSE in the source distribution or at
|
|
* https://www.openssl.org/source/license.html
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include "internal/cryptlib.h"
|
|
#include <openssl/buffer.h>
|
|
|
|
/*
|
|
* LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
|
|
* function is applied in several functions in this file and this limit
|
|
* ensures that the result fits in an int.
|
|
*/
|
|
#define LIMIT_BEFORE_EXPANSION 0x5ffffffc
|
|
|
|
BUF_MEM *BUF_MEM_new_ex(unsigned long flags)
|
|
{
|
|
BUF_MEM *ret;
|
|
|
|
ret = BUF_MEM_new();
|
|
if (ret != NULL)
|
|
ret->flags = flags;
|
|
return ret;
|
|
}
|
|
|
|
BUF_MEM *BUF_MEM_new(void)
|
|
{
|
|
BUF_MEM *ret;
|
|
|
|
ret = OPENSSL_zalloc(sizeof(*ret));
|
|
if (ret == NULL)
|
|
return NULL;
|
|
return ret;
|
|
}
|
|
|
|
void BUF_MEM_free(BUF_MEM *a)
|
|
{
|
|
if (a == NULL)
|
|
return;
|
|
if (a->data != NULL) {
|
|
if (a->flags & BUF_MEM_FLAG_SECURE)
|
|
OPENSSL_secure_clear_free(a->data, a->max);
|
|
else
|
|
OPENSSL_clear_free(a->data, a->max);
|
|
}
|
|
OPENSSL_free(a);
|
|
}
|
|
|
|
/* Allocate a block of secure memory; copy over old data if there
|
|
* was any, and then free it. */
|
|
static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
|
|
{
|
|
char *ret;
|
|
|
|
ret = OPENSSL_secure_malloc(len);
|
|
if (str->data != NULL) {
|
|
if (ret != NULL) {
|
|
memcpy(ret, str->data, str->length);
|
|
OPENSSL_secure_clear_free(str->data, str->length);
|
|
str->data = NULL;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
|
|
{
|
|
char *ret;
|
|
size_t n;
|
|
|
|
if (str->length >= len) {
|
|
str->length = len;
|
|
return len;
|
|
}
|
|
if (str->max >= len) {
|
|
if (str->data != NULL)
|
|
memset(&str->data[str->length], 0, len - str->length);
|
|
str->length = len;
|
|
return len;
|
|
}
|
|
/* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
|
|
if (len > LIMIT_BEFORE_EXPANSION) {
|
|
ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT);
|
|
return 0;
|
|
}
|
|
n = (len + 3) / 3 * 4;
|
|
if ((str->flags & BUF_MEM_FLAG_SECURE))
|
|
ret = sec_alloc_realloc(str, n);
|
|
else
|
|
ret = OPENSSL_realloc(str->data, n);
|
|
if (ret == NULL) {
|
|
len = 0;
|
|
} else {
|
|
str->data = ret;
|
|
str->max = n;
|
|
memset(&str->data[str->length], 0, len - str->length);
|
|
str->length = len;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
|
|
{
|
|
char *ret;
|
|
size_t n;
|
|
|
|
if (str->length >= len) {
|
|
if (str->data != NULL)
|
|
memset(&str->data[len], 0, str->length - len);
|
|
str->length = len;
|
|
return len;
|
|
}
|
|
if (str->max >= len) {
|
|
memset(&str->data[str->length], 0, len - str->length);
|
|
str->length = len;
|
|
return len;
|
|
}
|
|
/* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
|
|
if (len > LIMIT_BEFORE_EXPANSION) {
|
|
ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT);
|
|
return 0;
|
|
}
|
|
n = (len + 3) / 3 * 4;
|
|
if ((str->flags & BUF_MEM_FLAG_SECURE))
|
|
ret = sec_alloc_realloc(str, n);
|
|
else
|
|
ret = OPENSSL_clear_realloc(str->data, str->max, n);
|
|
if (ret == NULL) {
|
|
len = 0;
|
|
} else {
|
|
str->data = ret;
|
|
str->max = n;
|
|
memset(&str->data[str->length], 0, len - str->length);
|
|
str->length = len;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
|
|
{
|
|
size_t i;
|
|
if (in) {
|
|
out += size - 1;
|
|
for (i = 0; i < size; i++)
|
|
*out-- = *in++;
|
|
} else {
|
|
unsigned char *q;
|
|
char c;
|
|
q = out + size - 1;
|
|
for (i = 0; i < size / 2; i++) {
|
|
c = *q;
|
|
*q-- = *out;
|
|
*out++ = c;
|
|
}
|
|
}
|
|
}
|