Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions include/jose/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>

enum {
_JOSE_CFG_ERR_BASE = 0x1053000000000000ULL,
Expand Down Expand Up @@ -57,6 +58,23 @@ typedef struct jose_cfg jose_cfg_t;
typedef void (jose_cfg_err_t)(void *misc, const char *file, int line,
uint64_t err, const char *fmt, va_list ap);

/**
* Custom allocator function type signatures
*/
typedef void* (*jose_malloc_t)(size_t size);
typedef void* (*jose_realloc_t)(void *ptr, size_t size);
typedef void* (*jose_calloc_t)(size_t nmemb, size_t size);
typedef void (*jose_free_t)(void *ptr);

/**
* Global memory allocator function pointers
* These can be overridden using jose_set_alloc()
*/
extern jose_malloc_t jose_malloc;
extern jose_realloc_t jose_realloc;
extern jose_calloc_t jose_calloc;
extern jose_free_t jose_free;

/**
* Creates a new configuration instance.
*
Expand Down Expand Up @@ -134,4 +152,42 @@ jose_cfg_err(jose_cfg_t *cfg, const char *file, int line, uint64_t err,
jose_cfg_err(cfg, __FILE__, __LINE__, err, __VA_ARGS__)
#endif

/**
* Set custom memory allocator functions for JOSE operations globally.
*
* This allows you to override the default malloc/realloc/free used
* throughout the JOSE library. All JOSE operations will use these
* custom allocators once set. Useful for:
* - Memory debugging and tracking
* - Custom memory pools
* - Secure memory allocation
* - Cross-DLL memory management on Windows
*
* @param pmalloc Custom malloc function
* @param prealloc Custom realloc function
* @param pcalloc Custom calloc function
* @param pfree Custom free function
* @return 0 on success, errno on error
*/
int
jose_set_alloc(jose_malloc_t pmalloc, jose_realloc_t prealloc, jose_free_t pfree, jose_calloc_t pcalloc);

/**
* Get current memory allocator functions.
*
* @param pmalloc Pointer to store current malloc function
* @param prealloc Pointer to store current realloc function
* @param pcalloc Pointer to store current calloc function
* @param pfree Pointer to store current free function
*/
void
jose_get_alloc(jose_malloc_t *pmalloc, jose_realloc_t *prealloc, jose_free_t *pfree, jose_calloc_t *pcalloc);

/**
* Reset global allocators to system defaults.
* Useful for testing or when you want to stop using custom allocators.
*/
void
jose_reset_alloc(void);

/** @} */
18 changes: 9 additions & 9 deletions lib/b64.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ io_free(jose_io_t *io)
io_t *i = containerof(io, io_t, io);
jose_io_decref(i->next);
zero(i, sizeof(*i));
free(i);
jose_free(i);
}

static size_t
Expand Down Expand Up @@ -198,7 +198,7 @@ jose_b64_dec_io(jose_io_t *next)
jose_io_auto_t *io = NULL;
io_t *i = NULL;

i = calloc(1, sizeof(*i));
i = jose_calloc(1, sizeof(*i));
if (!i)
return NULL;

Expand Down Expand Up @@ -277,19 +277,19 @@ jose_b64_dec_load(const json_t *i)
if (size == SIZE_MAX)
return NULL;

buf = calloc(1, size);
buf = jose_calloc(1, size);
if (!buf)
return NULL;

if (jose_b64_dec(i, buf, size) != size) {
zero(buf, size);
free(buf);
jose_free(buf);
return NULL;
}

out = json_loadb((char *) buf, size, JSON_DECODE_ANY, NULL);
zero(buf, size);
free(buf);
jose_free(buf);
return out;
}

Expand All @@ -304,15 +304,15 @@ jose_b64_enc(const void *i, size_t il)
if (elen == SIZE_MAX)
return NULL;

enc = calloc(1, elen);
enc = jose_calloc(1, elen);
if (!enc)
return NULL;

if (jose_b64_enc_buf(i, il, enc, elen) == elen)
out = json_stringn(enc, elen);

zero(enc, elen);
free(enc);
jose_free(enc);
return out;
}

Expand All @@ -322,7 +322,7 @@ jose_b64_enc_io(jose_io_t *next)
jose_io_auto_t *io = NULL;
io_t *i = NULL;

i = calloc(1, sizeof(*i));
i = jose_calloc(1, sizeof(*i));
if (!i)
return NULL;

Expand Down Expand Up @@ -385,6 +385,6 @@ jose_b64_enc_dump(const json_t *i)

out = jose_b64_enc((const uint8_t *) buf, strlen(buf));
zero(buf, strlen(buf));
free(buf);
jose_free(buf);
return out;
}
48 changes: 46 additions & 2 deletions lib/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <jansson.h>

struct jose_cfg {
size_t refs;
Expand All @@ -30,6 +31,11 @@ struct jose_cfg {
void *misc;
};

jose_malloc_t jose_malloc = malloc;
jose_realloc_t jose_realloc = realloc;
jose_free_t jose_free = free;
jose_calloc_t jose_calloc = calloc;

static struct {
uint64_t nmbr;
const char *name;
Expand Down Expand Up @@ -78,7 +84,7 @@ jose_cfg_t *
jose_cfg(void)
{
jose_cfg_t *cfg = NULL;
cfg = calloc(1, sizeof(*cfg));
cfg = jose_calloc(1, sizeof(*cfg));
if (cfg)
*cfg = dflt;
return jose_cfg_incref(cfg);
Expand All @@ -104,7 +110,7 @@ void
jose_cfg_decref(jose_cfg_t *cfg)
{
if (cfg->refs-- == 1)
free(cfg);
jose_free(cfg);
}

void
Expand All @@ -131,3 +137,41 @@ jose_cfg_err(jose_cfg_t *cfg, const char *file, int line, uint64_t err,
c->err(c->misc, file, line, err, fmt, ap);
va_end(ap);
}

int
jose_set_alloc(jose_malloc_t pmalloc, jose_realloc_t prealloc, jose_free_t pfree, jose_calloc_t pcalloc)
{
/* all of the allocator functions must be set */
if (pmalloc == NULL || prealloc == NULL || pfree == NULL || pcalloc == NULL)
return EINVAL;

jose_malloc = pmalloc;
jose_realloc = prealloc;
jose_free = pfree;
jose_calloc = pcalloc;

/* Configure Jansson to use the same allocators as JOSE */
json_set_alloc_funcs(jose_malloc, jose_free);

return 0;
}

void
jose_get_alloc(jose_malloc_t *pmalloc, jose_realloc_t *prealloc, jose_free_t *pfree, jose_calloc_t *pcalloc)
{
if (pmalloc) *pmalloc = jose_malloc;
if (prealloc) *prealloc = jose_realloc;
if (pfree) *pfree = jose_free;
if (pcalloc) *pcalloc = jose_calloc;
}

void
jose_reset_alloc(void)
{
jose_malloc = malloc;
jose_realloc = realloc;
jose_free = free;
jose_calloc = calloc;
/* Reset Jansson to use default allocators */
json_set_alloc_funcs(malloc, free);
}
28 changes: 18 additions & 10 deletions lib/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ malloc_feed(jose_io_t *io, const void *in, size_t len)
if (len == 0)
return true;

tmp = realloc(*i->buf, *i->len + len);
tmp = jose_realloc(*i->buf, *i->len + len);
if (!tmp)
return false;

Expand All @@ -106,12 +106,12 @@ malloc_free(jose_io_t *io)

if (i->buf && *i->buf && i->len) {
zero(*i->buf, *i->len);
free(*i->buf);
jose_free(*i->buf);
*i->len = 0;
}

zero(i, sizeof(*i));
free(i);
jose_free(i);
}

jose_io_t *
Expand All @@ -123,10 +123,12 @@ jose_io_malloc(jose_cfg_t *cfg, void **buf, size_t *len)
if (!buf || !len)
return NULL;

i = calloc(1, sizeof(*i));
i = jose_malloc(sizeof(*i));
if (!i)
return NULL;

memset(i, 0, sizeof(*i));

io = jose_io_incref(&i->io);
io->feed = malloc_feed;
io->done = malloc_done;
Expand Down Expand Up @@ -172,7 +174,7 @@ buffer_free(jose_io_t *io)
{
io_buffer_t *i = containerof(io, io_buffer_t, io);
zero(i, sizeof(*i));
free(i);
jose_free(i);
}

jose_io_t *
Expand All @@ -184,10 +186,12 @@ jose_io_buffer(jose_cfg_t *cfg, void *buf, size_t *len)
if (!buf || !len)
return NULL;

i = calloc(1, sizeof(*i));
i = jose_malloc(sizeof(*i));
if (!i)
return NULL;

memset(i, 0, sizeof(*i));

io = jose_io_incref(&i->io);
io->feed = buffer_feed;
io->done = buffer_done;
Expand Down Expand Up @@ -219,7 +223,7 @@ file_free(jose_io_t *io)
{
io_file_t *i = containerof(io, io_file_t, io);
zero(i, sizeof(*i));
free(i);
jose_free(i);
}

jose_io_t *
Expand All @@ -231,10 +235,12 @@ jose_io_file(jose_cfg_t *cfg, FILE *file)
if (!file)
return NULL;

i = calloc(1, sizeof(*i));
i = jose_malloc(sizeof(*i));
if (!i)
return NULL;

memset(i, 0, sizeof(*i));

io = jose_io_incref(&i->io);
io->feed = file_feed;
io->done = file_done;
Expand Down Expand Up @@ -301,7 +307,7 @@ plex_free(jose_io_t *io)
jose_io_decref(i->nexts[j]);

zero(i, sizeof(*i));
free(i);
jose_free(i);
}

jose_io_t *
Expand All @@ -314,10 +320,12 @@ jose_io_multiplex(jose_cfg_t *cfg, jose_io_t **nexts, bool all)
while (nexts && nexts[nnexts])
nnexts++;

i = calloc(1, sizeof(*i) + sizeof(jose_io_t *) * nnexts);
i = jose_malloc(sizeof(*i) + sizeof(jose_io_t *) * nnexts);
if (!i)
return NULL;

memset(i, 0, sizeof(*i) + sizeof(jose_io_t *) * nnexts);

io = jose_io_incref(&i->io);
io->feed = plex_feed;
io->done = plex_done;
Expand Down
8 changes: 4 additions & 4 deletions lib/jws.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ ios_auto(jose_io_t ***iosp)
for (size_t i = 0; ios && ios[i]; i++)
jose_io_auto(&ios[i]);

free(ios);
jose_free(ios);
}

static jose_io_t *
Expand Down Expand Up @@ -183,7 +183,7 @@ jose_jws_sig_io(jose_cfg_t *cfg, json_t *jws, json_t *sig, const json_t *jwk)
if (json_is_array(sig) && json_array_size(sig) != json_array_size(jwk))
return NULL;

ios = calloc(json_array_size(jwk) + 1, sizeof(*ios));
ios = jose_calloc(json_array_size(jwk) + 1, sizeof(*ios));
if (!ios)
return NULL;

Expand Down Expand Up @@ -256,7 +256,7 @@ jose_jws_ver_io(jose_cfg_t *cfg, const json_t *jws, const json_t *sig,
if (json_is_array(sig) && json_array_size(sig) != json_array_size(jwk))
return NULL;

ios = calloc(json_array_size(jwk) + 1, sizeof(*ios));
ios = jose_calloc(json_array_size(jwk) + 1, sizeof(*ios));
if (!ios)
return NULL;

Expand Down Expand Up @@ -284,7 +284,7 @@ jose_jws_ver_io(jose_cfg_t *cfg, const json_t *jws, const json_t *sig,
if (!json_is_array(array))
return jose_jws_ver_io(cfg, jws, jws, jwk, true);

ios = calloc(json_array_size(array) + 1, sizeof(*ios));
ios = jose_calloc(json_array_size(array) + 1, sizeof(*ios));
if (!ios)
return NULL;

Expand Down
7 changes: 7 additions & 0 deletions lib/libjose.map
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ LIBJOSE_1.0 {
jose_cfg_get_err_misc;
jose_cfg_incref;
jose_cfg_set_err_func;
jose_set_alloc;
jose_get_alloc;
jose_malloc;
jose_calloc;
jose_realloc;
jose_free;
jose_reset_alloc;
jose_hook_alg_find;
jose_hook_alg_find_any;
jose_hook_alg_list;
Expand Down
Loading