From 2a4ea5843dd0f5b4d524a6fbae330184082fae84 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 17 Jan 2017 15:51:47 -0800 Subject: [PATCH] Added new openssl compatibility functions for BUF_MEM_new, BUF_MEM_grow and BUF_MEM_free. --- src/ssl.c | 67 ++++++++++++++++++++++++++++++++++++++ wolfssl/openssl/buffer.h | 37 +++++++++++++++++++++ wolfssl/openssl/include.am | 1 + wolfssl/ssl.h | 2 +- 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 wolfssl/openssl/buffer.h diff --git a/src/ssl.c b/src/ssl.c index 030379f46..adea86c1a 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -63,6 +63,7 @@ #include #include #include + #include #include #include #include @@ -25125,6 +25126,72 @@ WOLFSSL_DSA *wolfSSL_PEM_read_bio_DSAparams(WOLFSSL_BIO *bp, WOLFSSL_DSA **x, #define WOLFSSL_BIO_INCLUDED #include "src/bio.c" +/* Begin functions for openssl/buffer.h */ +WOLFSSL_BUF_MEM* wolfSSL_BUF_MEM_new(void) +{ + WOLFSSL_BUF_MEM* buf; + buf = (WOLFSSL_BUF_MEM*)XMALLOC(sizeof(WOLFSSL_BUF_MEM), NULL, + DYNAMIC_TYPE_OPENSSL); + if (buf) { + XMEMSET(buf, 0, sizeof(WOLFSSL_BUF_MEM)); + } + return buf; +} + +int wolfSSL_BUF_MEM_grow(WOLFSSL_BUF_MEM* buf, size_t len) +{ + size_t n; + + /* verify provided arguments */ + if (buf == NULL) { + return BAD_FUNC_ARG; + } + + /* check to see if buffer is already big enough */ + if (buf->length > len) { + buf->length = len; + return (int)len; + } + + /* check to see if buffer max fits */ + if (buf->max >= len) { + if (buf->data != NULL) { + XMEMSET(&buf->data[buf->length], 0, len - buf->length); + } + buf->length = len; + return (int)len; + } + + /* expand size, to handle growth */ + n = (len + 3) / 3 * 4; + + /* use realloc */ + buf->data = (char*)XREALLOC(buf->data, n, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (buf->data == NULL) { + return ERR_R_MALLOC_FAILURE; + } + + buf->max = n; + XMEMSET(&buf->data[buf->length], 0, len - buf->length); + buf->length = len; + + return (int)len; +} + +void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf) +{ + if (buf) { + if (buf->data) { + XFREE(buf->data, NULL, DYNAMIC_TYPE_TMP_BUFFER); + buf->data = NULL; + } + buf->max = 0; + buf->length = 0; + XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL); + } +} +/* End Functions for openssl/buffer.h */ + #endif /* OPENSSL_EXTRA */ diff --git a/wolfssl/openssl/buffer.h b/wolfssl/openssl/buffer.h new file mode 100644 index 000000000..353b04550 --- /dev/null +++ b/wolfssl/openssl/buffer.h @@ -0,0 +1,37 @@ +/* buffer.h for openssl */ + +#ifndef WOLFSSL_BUFFER_H_ +#define WOLFSSL_BUFFER_H_ + +#include +#include + +#ifdef __cplusplus + extern "C" { +#endif + +typedef struct WOLFSSL_BUF_MEM { + char* data; + size_t length; /* current length */ + size_t max; /* maximum length */ +} WOLFSSL_BUF_MEM; + + +WOLFSSL_API WOLFSSL_BUF_MEM* wolfSSL_BUF_MEM_new(void); +WOLFSSL_API int wolfSSL_BUF_MEM_grow(WOLFSSL_BUF_MEM* buf, size_t len); +WOLFSSL_API void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf); + + +#define BUF_MEM_new wolfSSL_BUF_MEM_new +#define BUF_MEM_grow wolfSSL_BUF_MEM_grow +#define BUF_MEM_free wolfSSL_BUF_MEM_free + +/* error codes */ +#define ERR_R_MALLOC_FAILURE MEMORY_E + + +#ifdef __cplusplus + } /* extern "C" */ +#endif + +#endif /* WOLFSSL_BUFFER_H_ */ diff --git a/wolfssl/openssl/include.am b/wolfssl/openssl/include.am index e4dcdf80a..563b35406 100644 --- a/wolfssl/openssl/include.am +++ b/wolfssl/openssl/include.am @@ -6,6 +6,7 @@ nobase_include_HEADERS+= \ wolfssl/openssl/aes.h\ wolfssl/openssl/bio.h \ wolfssl/openssl/bn.h \ + wolfssl/openssl/buffer.h \ wolfssl/openssl/conf.h \ wolfssl/openssl/crypto.h \ wolfssl/openssl/des.h \ diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 0a8754283..3b50c6fe8 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -175,7 +175,7 @@ typedef struct WOLFSSL_ASN1_STRING WOLFSSL_ASN1_STRING; typedef struct WOLFSSL_dynlock_value WOLFSSL_dynlock_value; typedef struct WOLFSSL_DH WOLFSSL_DH; typedef struct WOLFSSL_ASN1_BIT_STRING WOLFSSL_ASN1_BIT_STRING; -typedef unsigned char* WOLFSSL_BUF_MEM; +typedef struct WOLFSSL_BUF_MEM WOLFSSL_BUF_MEM; #define WOLFSSL_ASN1_UTCTIME WOLFSSL_ASN1_TIME #define WOLFSSL_ASN1_GENERALIZEDTIME WOLFSSL_ASN1_TIME