-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
source: Split jwt.c into logical files
Signed-off-by: Ben Collins <[email protected]>
- Loading branch information
1 parent
d5eead9
commit 46b4a51
Showing
17 changed files
with
1,522 additions
and
1,392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.