Added new openssl compatibility functions for BUF_MEM_new, BUF_MEM_grow and BUF_MEM_free.

This commit is contained in:
David Garske 2017-01-17 15:51:47 -08:00 committed by Jacob Barthelmeh
parent c237980d47
commit 2a4ea5843d
4 changed files with 106 additions and 1 deletions

View File

@ -63,6 +63,7 @@
#include <wolfssl/openssl/crypto.h>
#include <wolfssl/openssl/des.h>
#include <wolfssl/openssl/bn.h>
#include <wolfssl/openssl/buffer.h>
#include <wolfssl/openssl/dh.h>
#include <wolfssl/openssl/rsa.h>
#include <wolfssl/openssl/pem.h>
@ -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 */

37
wolfssl/openssl/buffer.h Normal file
View File

@ -0,0 +1,37 @@
/* buffer.h for openssl */
#ifndef WOLFSSL_BUFFER_H_
#define WOLFSSL_BUFFER_H_
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#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_ */

View File

@ -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 \

View File

@ -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