Skip to content

Commit

Permalink
Add the libb64 support from debian package maintainer
Browse files Browse the repository at this point in the history
Submitted-by: Thorsten Alteholz <[email protected]>
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Dec 9, 2024
1 parent 7e35485 commit 9a171e5
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 94 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$with_examples" != "xno"])
PKG_CHECK_MODULES([JANSSON], [jansson >= 2.0])
PKG_CHECK_MODULES([CHECK], [check >= 0.9.4], [true], [true])

AC_SEARCH_LIBS(base64_decode_value, b64,
AM_CONDITIONAL([HAVE_LIBB64], [true]), AM_CONDITIONAL([HAVE_LIBB64], [false]))

AX_VALGRIND_CHECK

AX_CODE_COVERAGE
Expand Down
10 changes: 8 additions & 2 deletions libjwt/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
EXTRA_DIST = base64.h jwt-private.h CMakeLists.txt
EXTRA_DIST = jwt-private.h CMakeLists.txt

lib_LTLIBRARIES = libjwt.la

libjwt_la_SOURCES = jwt.c base64.c
libjwt_la_SOURCES = jwt.c

# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
libjwt_la_LDFLAGS = -version-info 12:6:10 $(OPENSSL_LDFLAGS) $(GNUTLS_LDFLAGS) $(JANSSON_LDFLAGS) -no-undefined
Expand All @@ -20,5 +20,11 @@ libjwt_la_SOURCES += jwt-gnutls.c
libjwt_la_CPPFLAGS += -DHAVE_GNUTLS
endif

if HAVE_LIBB64
libjwt_la_SOURCES += jwt-b64.c
else
libjwt_la_SOURCES += base64.c
endif

pkgconfiglibdir = $(libdir)/pkgconfig
pkgconfiglib_DATA = libjwt.pc
90 changes: 0 additions & 90 deletions libjwt/base64.h

This file was deleted.

52 changes: 52 additions & 0 deletions libjwt/jwt-b64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright (C) 2019 Thorsten Alteholz <[email protected]>
Copyright (C) 2024 Ben Collins <[email protected]>
This file is part of the JWT C Library
SPDX-License-Identifier: MPL-2.0
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <string.h>

#include <b64/cencode.h>
#include <b64/cdecode.h>

int jwt_Base64encode(char *coded_dst, const char *plain_src, int len_plain_src)
{
base64_encodestate state;
int count, i, len;
char *rp = coded_dst, *wp = coded_dst;

base64_init_encodestate(&state);
count = base64_encode_block(plain_src, len_plain_src, coded_dst, &state);
count += base64_encode_blockend(coded_dst + count, &state);

/*
* the b64 library might insert \n after some chars,
* these must be removed again
* (at least in order to pass the tests)
*/
len = count;
for (i = 0; i < len; i++) {
if (*rp != '\n')
*wp++ = *rp;
else
count--;
rp++;
}
coded_dst[count] = 0;

return count;
}

int jwt_Base64decode(char *plain_dst, const char *coded_src)
{
base64_decodestate state;
int count = 0;

base64_init_decodestate(&state);
count = base64_decode_block(coded_src, strlen(coded_src), plain_dst, &state);

return count;
}
1 change: 0 additions & 1 deletion libjwt/jwt-gnutls.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <jwt.h>

#include "jwt-private.h"
#include "base64.h"
#include "config.h"

/* Workaround to use GnuTLS 3.5 EC signature encode/decode functions that
Expand Down
2 changes: 2 additions & 0 deletions libjwt/jwt-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void jwt_freemem(void *ptr);
/* Helper routines. */
void jwt_base64uri_encode(char *str);
void *jwt_b64_decode(const char *src, int *ret_len);
int jwt_Base64encode(char *coded_dst, const char *plain_src, int len_plain_src);
int jwt_Base64decode(char *plain_dst, const char *coded_src);

/* These routines are implemented by the crypto backend. */
int jwt_sign_sha_hmac(jwt_t *jwt, char **out, unsigned int *len,
Expand Down
1 change: 0 additions & 1 deletion libjwt/jwt.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <jwt.h>

#include "jwt-private.h"
#include "base64.h"
#include "config.h"

/* Library init functionality */
Expand Down

0 comments on commit 9a171e5

Please sign in to comment.