Skip to content

Commit

Permalink
source: Split jwt.c into logical files
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Dec 24, 2024
1 parent d5eead9 commit 46b4a51
Show file tree
Hide file tree
Showing 17 changed files with 1,522 additions and 1,392 deletions.
22 changes: 18 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ add_library(jwt SHARED)
add_library(jwt_static STATIC)
set_target_properties(jwt_static PROPERTIES OUTPUT_NAME jwt)

target_sources(jwt PRIVATE libjwt/jwt.c libjwt/jwks.c libjwt/base64.c)
target_sources(jwt PRIVATE
libjwt/base64.c
libjwt/jwt-memory.c
libjwt/jwt.c
libjwt/jwks.c
libjwt/jwt-setget.c
libjwt/jwt-crypto-ops.c
libjwt/jwt-validate.c
libjwt/jwt-encode.c
libjwt/jwt-verify.c)

set(COMPILER_CONSTRUCTOR "__attribute__((constructor))")
_check_c_compiler_attribute(${COMPILER_CONSTRUCTOR} COMPILER_HAS_CONSTRUCTOR)
Expand All @@ -69,7 +78,8 @@ endif()
generate_export_header(jwt
CUSTOM_CONTENT_FROM_VARIABLE JWT_CUSTOM_CONTENT)

include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR})
include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/libjwt)

target_link_libraries(jwt PUBLIC ${JANSSON_LINK_LIBRARIES})
target_include_directories(jwt PUBLIC
Expand All @@ -83,7 +93,9 @@ if (GNUTLS_FOUND)
target_link_libraries(jwt PUBLIC ${GNUTLS_LINK_LIBRARIES})
target_include_directories(jwt PUBLIC
${GNUTLS_INCLUDE_DIRS})
target_sources(jwt PRIVATE libjwt/jwt-gnutls.c libjwt/jwks-gnutls.c)
target_sources(jwt PRIVATE
libjwt/gnutls/jwk-parse.c
libjwt/gnutls/sign-verify.c)
endif()

if (OPENSSL_FOUND)
Expand All @@ -92,7 +104,9 @@ if (OPENSSL_FOUND)
target_link_libraries(jwt PUBLIC ${OPENSSL_LINK_LIBRARIES})
target_include_directories(jwt PUBLIC
${OPENSSL_INCLUDE_DIRS})
target_sources(jwt PRIVATE libjwt/jwt-openssl.c libjwt/jwks-openssl.c)
target_sources(jwt PRIVATE
libjwt/openssl/jwk-parse.c
libjwt/openssl/sign-verify.c)
endif()

# We need one of the things above to even work
Expand Down
10 changes: 6 additions & 4 deletions libjwt/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include

lib_LTLIBRARIES = libjwt.la

libjwt_la_SOURCES = jwt.c jwks.c base64.c jwt-private.h base64.h ll.h
libjwt_la_SOURCES = jwt.c jwt-verify.c jwt-encode.c jwks.c \
jwt-crypto-ops.c jwt-memory.c jwt-setget.c ../libjwt/jwt-validate.c \
base64.c jwt-private.h base64.h ll.h

libjwt_la_LDFLAGS = -version-info $(LIBJWT_VERSION_INFO) $(OPENSSL_LDFLAGS) \
$(GNUTLS_LDFLAGS) $(MBEDTLS_LIBS) $(JANSSON_LDFLAGS) -no-undefined
Expand All @@ -25,15 +27,15 @@ libjwt_la_LIBADD = $(JANSSON_LIBS) $(OPENSSL_LIBS) $(GNUTLS_LIBS) \
$(CODE_COVERAGE_LDFLAGS)

if HAVE_OPENSSL
libjwt_la_SOURCES += jwt-openssl.c jwks-openssl.c
libjwt_la_SOURCES += openssl/jwk-parse.c openssl/sign-verify.c
endif

if HAVE_GNUTLS
libjwt_la_SOURCES += jwt-gnutls.c jwks-gnutls.c
libjwt_la_SOURCES += gnutls/jwk-parse.c gnutls/sign-verify.c
endif

if HAVE_MBEDTLS
libjwt_la_SOURCES += jwt-mbedtls.c jwks-mbedtls.c
libjwt_la_SOURCES += mbedtls/jwk-parse.c mbedtls/sign-verify.c
endif

pkgconfiglibdir = $(libdir)/pkgconfig
Expand Down
File renamed without changes.
File renamed without changes.
112 changes: 112 additions & 0 deletions libjwt/jwt-crypto-ops.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* Copyright (C) 2015-2024 maClara, LLC <[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 <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

#include <jwt.h>

#include "jwt-private.h"

/* Library init functionality */
static struct jwt_crypto_ops *jwt_ops_available[] = {
#ifdef HAVE_OPENSSL
&jwt_openssl_ops,
#endif
#ifdef HAVE_GNUTLS
&jwt_gnutls_ops,
#endif
#ifdef HAVE_MBEDTLS
&jwt_mbedtls_ops,
#endif
NULL,
};

#if defined HAVE_OPENSSL
struct jwt_crypto_ops *jwt_ops = &jwt_openssl_ops;
#elif defined HAVE_GNUTLS
struct jwt_crypto_ops *jwt_ops = &jwt_gnutls_ops;
#elif defined HAVE_MBEDTLS
struct jwt_crypto_ops *jwt_ops = &jwt_mbedtls_ops;
#else
#error No crypto ops providers are enabled
#endif

const char *jwt_get_crypto_ops(void)
{
if (jwt_ops == NULL)
return "(unknown)"; // LCOV_EXCL_LINE

return jwt_ops->name;
}

jwt_crypto_provider_t jwt_get_crypto_ops_t(void)
{
if (jwt_ops == NULL)
return JWT_CRYPTO_OPS_NONE; // LCOV_EXCL_LINE

return jwt_ops->provider;
}

int jwt_set_crypto_ops_t(jwt_crypto_provider_t opname)
{
int i;

/* The user asked for something, let's give it a try */
for (i = 0; jwt_ops_available[i] != NULL; i++) {
if (jwt_ops_available[i]->provider != opname)
continue;

jwt_ops = jwt_ops_available[i];
return 0;
}

return EINVAL;
}

int jwt_set_crypto_ops(const char *opname)
{
int i;

/* The user asked for something, let's give it a try */
for (i = 0; jwt_ops_available[i] != NULL; i++) {
if (jwt_strcmp(jwt_ops_available[i]->name, opname))
continue;

jwt_ops = jwt_ops_available[i];
return 0;
}

return EINVAL;
}

int jwt_crypto_ops_supports_jwk(void)
{
return jwt_ops->jwk_implemented ? 1 : 0;
}

JWT_CONSTRUCTOR
void jwt_init()
{
const char *opname = getenv("JWT_CRYPTO");

/* By default, we choose the top spot */
if (opname == NULL || opname[0] == '\0') {
jwt_ops = jwt_ops_available[0];
return;
}

/* Attempt to set ops */
if (jwt_set_crypto_ops(opname)) {
jwt_ops = jwt_ops_available[0];
fprintf(stderr, "LibJWT: No such crypto ops [%s], falling back to [%s]\n",
opname, jwt_ops->name);
}
}
Loading

0 comments on commit 46b4a51

Please sign in to comment.