Skip to content

Commit

Permalink
Rename a number of BUF_* functions to OPENSSL_*.
Browse files Browse the repository at this point in the history
Upstream did this in 7644a9aef8932ed4d1c3f25ed776c997702982be, so align
with them. Add the new OPENSSL_* names and switch all callers witihn the
library to match. Keep the old BUF_* names around for compatibility.

Note there were two functions where we already had an OPENSSL_* version:
OPENSSL_strdup and OPENSSL_strnlen. The former now gains a NULL check to
align with BUF_strdup. The latter gets deduplicated; we had two
implementations.

Change-Id: Ia1cd4527a752fcd62e142ed1e1d7768d323279ba
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/38425
Reviewed-by: Adam Langley <[email protected]>
Commit-Queue: David Benjamin <[email protected]>
  • Loading branch information
davidben authored and CQ bot account: [email protected] committed Oct 21, 2019
1 parent 31f94b0 commit 3ba9586
Show file tree
Hide file tree
Showing 58 changed files with 171 additions and 192 deletions.
7 changes: 3 additions & 4 deletions crypto/asn1/a_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#include <time.h>

#include <openssl/asn1t.h>
#include <openssl/buf.h>
#include <openssl/err.h>
#include <openssl/mem.h>

Expand Down Expand Up @@ -143,11 +142,11 @@ ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
str = (char *)ret->data;
/* Work out the century and prepend */
if (t->data[0] >= '5')
BUF_strlcpy(str, "19", newlen);
OPENSSL_strlcpy(str, "19", newlen);
else
BUF_strlcpy(str, "20", newlen);
OPENSSL_strlcpy(str, "20", newlen);

BUF_strlcat(str, (char *)t->data, newlen);
OPENSSL_strlcat(str, (char *)t->data, newlen);

done:
if (out != NULL && *out == NULL)
Expand Down
7 changes: 3 additions & 4 deletions crypto/bio/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ OPENSSL_MSVC_PRAGMA(warning(push, 3))
OPENSSL_MSVC_PRAGMA(warning(pop))
#endif

#include <openssl/buf.h>
#include <openssl/err.h>
#include <openssl/mem.h>

Expand Down Expand Up @@ -149,7 +148,7 @@ static int split_host_and_port(char **out_host, char **out_port, const char *nam
}
}

*out_host = BUF_strndup(host, host_len);
*out_host = OPENSSL_strndup(host, host_len);
if (*out_host == NULL) {
return 0;
}
Expand Down Expand Up @@ -429,13 +428,13 @@ static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
bio->init = 1;
if (num == 0) {
OPENSSL_free(data->param_hostname);
data->param_hostname = BUF_strdup(ptr);
data->param_hostname = OPENSSL_strdup(ptr);
if (data->param_hostname == NULL) {
ret = 0;
}
} else if (num == 1) {
OPENSSL_free(data->param_port);
data->param_port = BUF_strdup(ptr);
data->param_port = OPENSSL_strdup(ptr);
if (data->param_port == NULL) {
ret = 0;
}
Expand Down
1 change: 0 additions & 1 deletion crypto/bio/fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ OPENSSL_MSVC_PRAGMA(warning(push, 3))
OPENSSL_MSVC_PRAGMA(warning(pop))
#endif

#include <openssl/buf.h>
#include <openssl/err.h>
#include <openssl/mem.h>

Expand Down
11 changes: 5 additions & 6 deletions crypto/bio/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
#include <stdio.h>
#include <string.h>

#include <openssl/buf.h>
#include <openssl/err.h>
#include <openssl/mem.h>

Expand Down Expand Up @@ -208,16 +207,16 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
b->shutdown = (int)num & BIO_CLOSE;
if (num & BIO_FP_APPEND) {
if (num & BIO_FP_READ) {
BUF_strlcpy(p, "a+", sizeof(p));
OPENSSL_strlcpy(p, "a+", sizeof(p));
} else {
BUF_strlcpy(p, "a", sizeof(p));
OPENSSL_strlcpy(p, "a", sizeof(p));
}
} else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
BUF_strlcpy(p, "r+", sizeof(p));
OPENSSL_strlcpy(p, "r+", sizeof(p));
} else if (num & BIO_FP_WRITE) {
BUF_strlcpy(p, "w", sizeof(p));
OPENSSL_strlcpy(p, "w", sizeof(p));
} else if (num & BIO_FP_READ) {
BUF_strlcpy(p, "r", sizeof(p));
OPENSSL_strlcpy(p, "r", sizeof(p));
} else {
OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE);
ret = 0;
Expand Down
1 change: 0 additions & 1 deletion crypto/bio/pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
#include <assert.h>
#include <string.h>

#include <openssl/buf.h>
#include <openssl/err.h>
#include <openssl/mem.h>

Expand Down
75 changes: 6 additions & 69 deletions crypto/buf/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,87 +145,24 @@ int BUF_MEM_append(BUF_MEM *buf, const void *in, size_t len) {
return 1;
}

char *BUF_strdup(const char *str) {
if (str == NULL) {
return NULL;
}

return BUF_strndup(str, strlen(str));
}
char *BUF_strdup(const char *str) { return OPENSSL_strdup(str); }

size_t BUF_strnlen(const char *str, size_t max_len) {
size_t i;

for (i = 0; i < max_len; i++) {
if (str[i] == 0) {
break;
}
}

return i;
return OPENSSL_strnlen(str, max_len);
}

char *BUF_strndup(const char *str, size_t size) {
char *ret;
size_t alloc_size;

if (str == NULL) {
return NULL;
}

size = BUF_strnlen(str, size);

alloc_size = size + 1;
if (alloc_size < size) {
// overflow
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
return NULL;
}
ret = OPENSSL_malloc(alloc_size);
if (ret == NULL) {
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
return NULL;
}

OPENSSL_memcpy(ret, str, size);
ret[size] = '\0';
return ret;
return OPENSSL_strndup(str, size);
}

size_t BUF_strlcpy(char *dst, const char *src, size_t dst_size) {
size_t l = 0;

for (; dst_size > 1 && *src; dst_size--) {
*dst++ = *src++;
l++;
}

if (dst_size) {
*dst = 0;
}

return l + strlen(src);
return OPENSSL_strlcpy(dst, src, dst_size);
}

size_t BUF_strlcat(char *dst, const char *src, size_t dst_size) {
size_t l = 0;
for (; dst_size > 0 && *dst; dst_size--, dst++) {
l++;
}
return l + BUF_strlcpy(dst, src, dst_size);
return OPENSSL_strlcat(dst, src, dst_size);
}

void *BUF_memdup(const void *data, size_t size) {
if (size == 0) {
return NULL;
}

void *ret = OPENSSL_malloc(size);
if (ret == NULL) {
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
return NULL;
}

OPENSSL_memcpy(ret, data, size);
return ret;
return OPENSSL_memdup(data, size);
}
3 changes: 1 addition & 2 deletions crypto/bytestring/cbb.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <limits.h>
#include <string.h>

#include <openssl/buf.h>
#include <openssl/mem.h>

#include "../internal.h"
Expand Down Expand Up @@ -649,7 +648,7 @@ int CBB_flush_asn1_set_of(CBB *cbb) {
// remain valid as we rewrite |cbb|.
int ret = 0;
size_t buf_len = CBB_len(cbb);
uint8_t *buf = BUF_memdup(CBB_data(cbb), buf_len);
uint8_t *buf = OPENSSL_memdup(CBB_data(cbb), buf_len);
CBS *children = OPENSSL_malloc(num_children * sizeof(CBS));
if (buf == NULL || children == NULL) {
goto err;
Expand Down
5 changes: 2 additions & 3 deletions crypto/bytestring/cbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <openssl/buf.h>
#include <openssl/mem.h>
#include <openssl/bytestring.h>

Expand Down Expand Up @@ -61,7 +60,7 @@ int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
if (cbs->len == 0) {
return 1;
}
*out_ptr = BUF_memdup(cbs->data, cbs->len);
*out_ptr = OPENSSL_memdup(cbs->data, cbs->len);
if (*out_ptr == NULL) {
return 0;
}
Expand All @@ -73,7 +72,7 @@ int CBS_strdup(const CBS *cbs, char **out_ptr) {
if (*out_ptr != NULL) {
OPENSSL_free(*out_ptr);
}
*out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
*out_ptr = OPENSSL_strndup((const char*)cbs->data, cbs->len);
return (*out_ptr != NULL);
}

Expand Down
1 change: 0 additions & 1 deletion crypto/cpu-arm-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <unistd.h>

#include <openssl/arm_arch.h>
#include <openssl/buf.h>
#include <openssl/mem.h>

#include "cpu-arm-linux.h"
Expand Down
3 changes: 1 addition & 2 deletions crypto/dh/dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#include <string.h>

#include <openssl/bn.h>
#include <openssl/buf.h>
#include <openssl/err.h>
#include <openssl/ex_data.h>
#include <openssl/mem.h>
Expand Down Expand Up @@ -476,7 +475,7 @@ static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
to->seedlen = 0;

if (from->seed) {
to->seed = BUF_memdup(from->seed, from->seedlen);
to->seed = OPENSSL_memdup(from->seed, from->seedlen);
if (!to->seed) {
return 0;
}
Expand Down
5 changes: 2 additions & 3 deletions crypto/ec_extra/ec_derive.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <string.h>

#include <openssl/buf.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/digest.h>
Expand All @@ -40,8 +39,8 @@ EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret,
// separated.
static const char kLabel[] = "derive EC key ";
char info[sizeof(kLabel) + EC_KEY_DERIVE_MAX_NAME_LEN];
BUF_strlcpy(info, kLabel, sizeof(info));
BUF_strlcat(info, name, sizeof(info));
OPENSSL_strlcpy(info, kLabel, sizeof(info));
OPENSSL_strlcat(info, name, sizeof(info));

// Generate 128 bits beyond the group order so the bias is at most 2^-128.
#define EC_KEY_DERIVE_EXTRA_BITS 128
Expand Down
5 changes: 2 additions & 3 deletions crypto/evp/evp_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ OPENSSL_MSVC_PRAGMA(warning(pop))

#include <gtest/gtest.h>

#include <openssl/buf.h>
#include <openssl/bytestring.h>
#include <openssl/crypto.h>
#include <openssl/digest.h>
Expand Down Expand Up @@ -280,8 +279,8 @@ static bool SetupContext(FileTest *t, KeyMap *key_map, EVP_PKEY_CTX *ctx) {
}
// For historical reasons, |EVP_PKEY_CTX_set0_rsa_oaep_label| expects to be
// take ownership of the input.
bssl::UniquePtr<uint8_t> buf(
reinterpret_cast<uint8_t *>(BUF_memdup(label.data(), label.size())));
bssl::UniquePtr<uint8_t> buf(reinterpret_cast<uint8_t *>(
OPENSSL_memdup(label.data(), label.size())));
if (!buf ||
!EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, buf.get(), label.size())) {
return false;
Expand Down
1 change: 0 additions & 1 deletion crypto/evp/p_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#include <string.h>

#include <openssl/bn.h>
#include <openssl/buf.h>
#include <openssl/digest.h>
#include <openssl/ec.h>
#include <openssl/ec_key.h>
Expand Down
3 changes: 1 addition & 2 deletions crypto/evp/p_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#include <string.h>

#include <openssl/bn.h>
#include <openssl/buf.h>
#include <openssl/bytestring.h>
#include <openssl/digest.h>
#include <openssl/err.h>
Expand Down Expand Up @@ -135,7 +134,7 @@ static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
dctx->saltlen = sctx->saltlen;
if (sctx->oaep_label) {
OPENSSL_free(dctx->oaep_label);
dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
if (!dctx->oaep_label) {
return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions crypto/evp/p_x25519_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include <openssl/evp.h>

#include <openssl/buf.h>
#include <openssl/bytestring.h>
#include <openssl/curve25519.h>
#include <openssl/err.h>
Expand Down Expand Up @@ -244,6 +243,6 @@ size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) {
return 0;
}

*out_ptr = BUF_memdup(key->pub, 32);
*out_ptr = OPENSSL_memdup(key->pub, 32);
return *out_ptr == NULL ? 0 : 32;
}
Loading

0 comments on commit 3ba9586

Please sign in to comment.