diff --git a/configure.ac b/configure.ac
index b60257d..d14e72c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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
diff --git a/libjwt/Makefile.am b/libjwt/Makefile.am
index 1d78435..b08af2b 100644
--- a/libjwt/Makefile.am
+++ b/libjwt/Makefile.am
@@ -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
@@ -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
diff --git a/libjwt/base64.h b/libjwt/base64.h
deleted file mode 100644
index 645c9d7..0000000
--- a/libjwt/base64.h
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- *
- * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
- *
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
- *
- * @APPLE_LICENSE_HEADER_END@
- */
-/* ====================================================================
- * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. All advertising materials mentioning features or use of this
- * software must display the following acknowledgment:
- * "This product includes software developed by the Apache Group
- * for use in the Apache HTTP server project (http://www.apache.org/)."
- *
- * 4. The names "Apache Server" and "Apache Group" must not be used to
- * endorse or promote products derived from this software without
- * prior written permission. For written permission, please contact
- * apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache"
- * nor may "Apache" appear in their names without prior written
- * permission of the Apache Group.
- *
- * 6. Redistributions of any form whatsoever must retain the following
- * acknowledgment:
- * "This product includes software developed by the Apache Group
- * for use in the Apache HTTP server project (http://www.apache.org/)."
- *
- * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Group and was originally based
- * on public domain software written at the National Center for
- * Supercomputing Applications, University of Illinois, Urbana-Champaign.
- * For more information on the Apache Group and the Apache HTTP server
- * project, please see .
- *
- */
-
-
-
-#ifndef _JWT_BASE64_H_
-#define _JWT_BASE64_H_
-
-int jwt_Base64encode(char *coded_dst, const char *plain_src, int len_plain_src);
-int jwt_Base64decode(char *plain_dst, const char *coded_src);
-
-#endif /* _JWT_BASE64_H_ */
diff --git a/libjwt/jwt-b64.c b/libjwt/jwt-b64.c
new file mode 100644
index 0000000..1c2a370
--- /dev/null
+++ b/libjwt/jwt-b64.c
@@ -0,0 +1,52 @@
+/* Copyright (C) 2019 Thorsten Alteholz
+ Copyright (C) 2024 Ben Collins
+ 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
+
+#include
+#include
+
+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;
+}
diff --git a/libjwt/jwt-gnutls.c b/libjwt/jwt-gnutls.c
index b9416ea..182ee76 100644
--- a/libjwt/jwt-gnutls.c
+++ b/libjwt/jwt-gnutls.c
@@ -18,7 +18,6 @@
#include
#include "jwt-private.h"
-#include "base64.h"
#include "config.h"
/* Workaround to use GnuTLS 3.5 EC signature encode/decode functions that
diff --git a/libjwt/jwt-private.h b/libjwt/jwt-private.h
index 87fb2d1..98ca386 100644
--- a/libjwt/jwt-private.h
+++ b/libjwt/jwt-private.h
@@ -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,
diff --git a/libjwt/jwt.c b/libjwt/jwt.c
index d07fa8b..88a4e59 100644
--- a/libjwt/jwt.c
+++ b/libjwt/jwt.c
@@ -14,7 +14,6 @@
#include
#include "jwt-private.h"
-#include "base64.h"
#include "config.h"
/* Library init functionality */