Skip to content

Commit

Permalink
Support custom memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwolf committed Apr 3, 2016
1 parent 56e5182 commit c77d033
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 97 deletions.
1 change: 1 addition & 0 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ nobase_include_HEADERS = cjose/cjose.h \
cjose/jws.h \
cjose/header.h \
cjose/error.h \
cjose/util.h \
cjose/version.h
1 change: 1 addition & 0 deletions include/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ nobase_include_HEADERS = cjose/cjose.h \
cjose/jws.h \
cjose/header.h \
cjose/error.h \
cjose/util.h \
cjose/version.h

all: all-am
Expand Down
87 changes: 87 additions & 0 deletions include/cjose/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyrights
*
* Portions created or assigned to Cisco Systems, Inc. are
* Copyright (c) 2014-2016 Cisco Systems, Inc. All Rights Reserved.
*/

/**
* \file util.h
* \brief Utility functions and data structures for CJOSE.
*
*/

#ifndef CJOSE_UTIL_H
#define CJOSE_UTIL_H

#include <stddef.h>

#ifdef __cplusplus
extern "C"
{
#endif


/**
* Typedef for memory allocator function.
*/
typedef void *(* cjose_alloc_fn_t)(size_t);

/**
* Typedef for memory reallocator function.
*/
typedef void *(* cjose_realloc_fn_t)(void *, size_t);

/**
* Typedef for memory deallocator function.
*/
typedef void (* cjose_dealloc_fn_t)(void *);

/**
* Sets the allocator and deallocator functions.
*
* If <tt>alloc</tt> is NULL, any previously set allocator function is clared
* and the the default allocator <tt>malloc()</tt>
* is used.
*
* If <tt>dealloc</tt> is NULL, the default dallocator <tt>free()</tt>
* is used.
*
* \param alloc [in] The custom allocator function to use.
* \param realloc [in] The custom reallocator function to use.
* \param dealloc [in] The custom deallocator function to use.
*/
void cjose_set_alloc_funcs(cjose_alloc_fn_t alloc,
cjose_realloc_fn_t realloc,
cjose_dealloc_fn_t dealloc);


/**
* Retrieves the configured allocator function. If an allocator function is
* not set, this function returns a pointer to <tt>malloc()</tt>.
*
* \returns The configured allocator function
*/
cjose_alloc_fn_t cjose_get_alloc();

/**
* Retrieve the configured reallocator function. If a reallocator function is
* not set, this function retursn a pointer to <tt>realloc</tt>.
*
* \returns The configured reallocator function
*/
cjose_realloc_fn_t cjose_get_realloc();

/**
* Retrieves the configured deallocator function. If a deallocator function is
* not set, this function returns a pointer to <tt>free()</tt>.
*
* \returns The configured deallocator function
*/
cjose_dealloc_fn_t cjose_get_dealloc();

#ifdef __cplusplus
}
#endif

#endif // CJOSE_UTIL_H
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ AM_CFLAGS =-std=gnu99 --pedantic -Wall -Werror -g -O2 -I$(top_builddir)/include
lib_LTLIBRARIES=libcjose.la
libcjose_la_CPPFLAGS= -I$(topdir)/include
libcjose_la_SOURCES=version.c \
util.c \
base64.c \
jwk.c \
jwe.c \
Expand Down
15 changes: 12 additions & 3 deletions src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ am__uninstall_files_from_dir = { \
am__installdirs = "$(DESTDIR)$(libdir)"
LTLIBRARIES = $(lib_LTLIBRARIES)
libcjose_la_LIBADD =
am_libcjose_la_OBJECTS = libcjose_la-version.lo libcjose_la-base64.lo \
libcjose_la-jwk.lo libcjose_la-jwe.lo libcjose_la-jws.lo \
libcjose_la-header.lo libcjose_la-error.lo
am_libcjose_la_OBJECTS = libcjose_la-version.lo libcjose_la-util.lo \
libcjose_la-base64.lo libcjose_la-jwk.lo libcjose_la-jwe.lo \
libcjose_la-jws.lo libcjose_la-header.lo libcjose_la-error.lo
libcjose_la_OBJECTS = $(am_libcjose_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
Expand Down Expand Up @@ -348,6 +348,7 @@ AM_CFLAGS = -std=gnu99 --pedantic -Wall -Werror -g -O2 -I$(top_builddir)/include
lib_LTLIBRARIES = libcjose.la
libcjose_la_CPPFLAGS = -I$(topdir)/include
libcjose_la_SOURCES = version.c \
util.c \
base64.c \
jwk.c \
jwe.c \
Expand Down Expand Up @@ -443,6 +444,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcjose_la-jwe.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcjose_la-jwk.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcjose_la-jws.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcjose_la-util.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcjose_la-version.Plo@am__quote@

.c.o:
Expand Down Expand Up @@ -473,6 +475,13 @@ libcjose_la-version.lo: version.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcjose_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libcjose_la-version.lo `test -f 'version.c' || echo '$(srcdir)/'`version.c

libcjose_la-util.lo: util.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcjose_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libcjose_la-util.lo -MD -MP -MF $(DEPDIR)/libcjose_la-util.Tpo -c -o libcjose_la-util.lo `test -f 'util.c' || echo '$(srcdir)/'`util.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libcjose_la-util.Tpo $(DEPDIR)/libcjose_la-util.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='util.c' object='libcjose_la-util.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcjose_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libcjose_la-util.lo `test -f 'util.c' || echo '$(srcdir)/'`util.c

libcjose_la-base64.lo: base64.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcjose_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libcjose_la-base64.lo -MD -MP -MF $(DEPDIR)/libcjose_la-base64.Tpo -c -o libcjose_la-base64.lo `test -f 'base64.c' || echo '$(srcdir)/'`base64.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libcjose_la-base64.Tpo $(DEPDIR)/libcjose_la-base64.Plo
Expand Down
13 changes: 7 additions & 6 deletions src/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <cjose/base64.h>
#include <cjose/util.h>

#include <errno.h>
#include <string.h>
Expand Down Expand Up @@ -73,7 +74,7 @@ static inline bool _decode(const char *input, size_t inlen,
// return empty string on 0 length input
if (0 == inlen)
{
uint8_t *retVal = (uint8_t *)malloc(sizeof(uint8_t));
uint8_t *retVal = (uint8_t *)cjose_get_alloc()(sizeof(uint8_t));
if (NULL == retVal)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
Expand All @@ -96,7 +97,7 @@ static inline bool _decode(const char *input, size_t inlen,
// rlen takes a best guess on size;
// might be too large for base64url, but never too small.
size_t rlen = ((inlen * 3) >> 2) + 3;
uint8_t *buffer = malloc(sizeof(uint8_t) * rlen);
uint8_t *buffer = cjose_get_alloc()(sizeof(uint8_t) * rlen);
if (NULL == buffer)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
Expand Down Expand Up @@ -130,7 +131,7 @@ static inline bool _decode(const char *input, size_t inlen,
if (0xff == val)
{
CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
free(buffer);
cjose_get_dealloc()(buffer);
return false;
}
idx++;
Expand Down Expand Up @@ -168,7 +169,7 @@ static inline bool _decode(const char *input, size_t inlen,
b64_decode_failed:
if (NULL != buffer)
{
free(buffer);
cjose_get_dealloc()(buffer);
}
return false;
}
Expand All @@ -186,7 +187,7 @@ static inline bool _encode(const uint8_t *input, size_t inlen,
// return empty string on 0 length input
if (!inlen)
{
char * retVal = (char *)malloc(sizeof(char));
char * retVal = (char *)cjose_get_alloc()(sizeof(char));
if (!retVal)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
Expand All @@ -202,7 +203,7 @@ static inline bool _encode(const uint8_t *input, size_t inlen,
size_t rlen = (((inlen + 2) / 3) << 2);
char *base;

base = (char *)malloc(sizeof(char) * (rlen+1));
base = (char *)cjose_get_alloc()(sizeof(char) * (rlen+1));
if (NULL == base)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
Expand Down
41 changes: 22 additions & 19 deletions src/jwe.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
* Copyright (c) 2014-2016 Cisco Systems, Inc. All Rights Reserved.
*/

#include <cjose/base64.h>
#include <cjose/header.h>
#include <cjose/jwe.h>
#include <cjose/util.h>

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include "cjose/jwe.h"
#include "cjose/header.h"
#include "cjose/base64.h"

#include "include/header_int.h"
#include "include/jwk_int.h"
#include "include/jwe_int.h"
Expand Down Expand Up @@ -66,7 +69,7 @@ static bool _cjose_jwe_malloc(
uint8_t **buffer,
cjose_err *err)
{
*buffer = (uint8_t *)malloc(bytes);
*buffer = (uint8_t *)cjose_get_alloc()(bytes);
if (NULL == *buffer)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
Expand Down Expand Up @@ -108,11 +111,11 @@ static bool _cjose_jwe_build_hdr(
if (NULL == jwe->part[0].raw)
{
CJOSE_ERROR(err, CJOSE_ERR_NO_MEMORY);
free(hdr_str);
cjose_get_dealloc()(hdr_str);
return false;
}
jwe->part[0].raw_len = strlen(hdr_str);
free(hdr_str);
cjose_get_dealloc()(hdr_str);

return true;
}
Expand Down Expand Up @@ -189,7 +192,7 @@ static bool _cjose_jwe_set_cek_a256gcm(
// if no JWK is provided, generate a random key
if (NULL == jwk)
{
free(jwe->cek);
cjose_get_dealloc()(jwe->cek);
if (!_cjose_jwe_malloc(keysize, true, &jwe->cek, err))
{
return false;
Expand All @@ -208,7 +211,7 @@ static bool _cjose_jwe_set_cek_a256gcm(
}

// copy the key material directly from jwk to the jwe->cek
free(jwe->cek);
cjose_get_dealloc()(jwe->cek);
if (!_cjose_jwe_malloc(keysize, false, &jwe->cek, err))
{
return false;
Expand Down Expand Up @@ -286,7 +289,7 @@ static bool _cjose_jwe_encrypt_ek_rsa_oaep(
}

// allocate memory for RSA encryption
free(jwe->part[1].raw);
cjose_get_dealloc()(jwe->part[1].raw);
if (!_cjose_jwe_malloc(jwe->part[1].raw_len, false, &jwe->part[1].raw, err))
{
return false;
Expand Down Expand Up @@ -325,7 +328,7 @@ static bool _cjose_jwe_decrypt_ek_rsa_oaep(
}

// we don't know the size of the key to expect, but must be < RSA_size
free(jwe->cek);
cjose_get_dealloc()(jwe->cek);
size_t buflen = RSA_size((RSA *)jwk->keydata);
if (!_cjose_jwe_malloc(buflen, false, &jwe->cek, err))
{
Expand All @@ -352,7 +355,7 @@ static bool _cjose_jwe_set_iv_a256gcm(
cjose_err *err)
{
// generate IV as random 96 bit value
free(jwe->part[2].raw);
cjose_get_dealloc()(jwe->part[2].raw);
jwe->part[2].raw_len = 12;
if (!_cjose_jwe_malloc(jwe->part[2].raw_len, true, &jwe->part[2].raw, err))
{
Expand Down Expand Up @@ -423,7 +426,7 @@ static bool _cjose_jwe_encrypt_dat_a256gcm(
}

// allocate buffer for the ciphertext
free(jwe->part[3].raw);
cjose_get_dealloc()(jwe->part[3].raw);
jwe->part[3].raw_len = plaintext_len;
if (!_cjose_jwe_malloc(jwe->part[3].raw_len, false, &jwe->part[3].raw, err))
{
Expand All @@ -448,7 +451,7 @@ static bool _cjose_jwe_encrypt_dat_a256gcm(
}

// allocate buffer for the authentication tag
free(jwe->part[4].raw);
cjose_get_dealloc()(jwe->part[4].raw);
jwe->part[4].raw_len = 16;
if (!_cjose_jwe_malloc(jwe->part[4].raw_len, false, &jwe->part[4].raw, err))
{
Expand Down Expand Up @@ -527,7 +530,7 @@ static bool _cjose_jwe_decrypt_dat_a256gcm(
}

// allocate buffer for the plaintext
free(jwe->dat);
cjose_get_dealloc()(jwe->dat);
jwe->dat_len = jwe->part[3].raw_len;
if (!_cjose_jwe_malloc(jwe->dat_len, false, &jwe->dat, err))
{
Expand Down Expand Up @@ -644,12 +647,12 @@ void cjose_jwe_release(
}
for (int i = 0; i < 5; ++i)
{
free(jwe->part[i].raw);
free(jwe->part[i].b64u);
cjose_get_dealloc()(jwe->part[i].raw);
cjose_get_dealloc()(jwe->part[i].b64u);
}
free(jwe->cek);
free(jwe->dat);
free(jwe);
cjose_get_dealloc()(jwe->cek);
cjose_get_dealloc()(jwe->dat);
cjose_get_dealloc()(jwe);
}


Expand Down
Loading

0 comments on commit c77d033

Please sign in to comment.