Make JWT C again

Return to C since we don't need to replace the JasmineJSON library to the RapidJSON one, as the signature gets verified first before any JSON is getting parsed. JasmineJSON also appears to only have problems when compiled in the STRICT and/or LINK mode, which we aren't doing.
This commit is contained in:
Kawe Mazidjatari 2023-10-12 17:03:12 +02:00
parent d83270a541
commit 1783df407b
14 changed files with 88 additions and 100 deletions

View File

@ -4,12 +4,12 @@ add_module( "lib" "libjwt" "" ${FOLDER_CONTEXT} TRUE TRUE )
start_sources()
add_sources( SOURCE_GROUP "Source"
"base64.cpp"
"claim.cpp"
"decode.cpp"
"encode.cpp"
"util.cpp"
"version.cpp"
"base64.c"
"claim.c"
"decode.c"
"encode.c"
"util.c"
"version.c"
)
add_sources( SOURCE_GROUP "Include"

View File

@ -14,9 +14,9 @@
limitations under the License.
*/
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "include/claim.h"
#include "include/version.h"
@ -24,13 +24,7 @@
#include "include/chillbuff.h"
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <mbedtls/md.h>
#ifdef __cplusplus
}
#endif
void l8w8jwt_free_claims(struct l8w8jwt_claim* claims, const size_t claims_count)
{
@ -147,6 +141,6 @@ struct l8w8jwt_claim* l8w8jwt_get_claim(struct l8w8jwt_claim* claims, const size
return NULL;
}
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -14,9 +14,9 @@
limitations under the License.
*/
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#define JSMN_STATIC
@ -30,9 +30,6 @@
#include <string.h>
#include <inttypes.h>
//#ifdef __cplusplus
//extern "C" {
//#endif
#include <mbedtls/pk.h>
#include <mbedtls/md.h>
#include <mbedtls/platform_util.h>
@ -40,9 +37,6 @@
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/pk.h>
#include <mbedtls/x509_crt.h>
//#ifdef __cplusplus
//}
//#endif
#if L8W8JWT_ENABLE_EDDSA
#include <ed25519.h>
@ -335,7 +329,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
return L8W8JWT_NULL_ARG;
}
*out_validation_result = (l8w8jwt_validation_result)~L8W8JWT_VALID;
*out_validation_result = (enum l8w8jwt_validation_result)~L8W8JWT_VALID;
char* header = NULL;
size_t header_length = 0;
@ -661,7 +655,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_sub != NULL)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "sub", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "sub", 3);
if (c == NULL || strncmp(c->value, params->validate_sub, params->validate_sub_length ? params->validate_sub_length : strlen(params->validate_sub)) != 0)
{
validation_res |= (unsigned)L8W8JWT_SUB_FAILURE;
@ -670,7 +664,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_aud != NULL)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "aud", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "aud", 3);
if (c == NULL || strncmp(c->value, params->validate_aud, params->validate_aud_length ? params->validate_aud_length : strlen(params->validate_aud)) != 0)
{
validation_res |= (unsigned)L8W8JWT_AUD_FAILURE;
@ -679,7 +673,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_iss != NULL)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "iss", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "iss", 3);
if (c == NULL || strncmp(c->value, params->validate_iss, params->validate_iss_length ? params->validate_iss_length : strlen(params->validate_iss)) != 0)
{
validation_res |= (unsigned)L8W8JWT_ISS_FAILURE;
@ -688,7 +682,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_jti != NULL)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "jti", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "jti", 3);
if (c == NULL || strncmp(c->value, params->validate_jti, params->validate_jti_length ? params->validate_jti_length : strlen(params->validate_jti)) != 0)
{
validation_res |= (unsigned)L8W8JWT_JTI_FAILURE;
@ -699,7 +693,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_exp)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "exp", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "exp", 3);
if (c == NULL || ct - params->exp_tolerance_seconds > strtoll(c->value, NULL, 10))
{
validation_res |= (unsigned)L8W8JWT_EXP_FAILURE;
@ -708,7 +702,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_nbf)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "nbf", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "nbf", 3);
if (c == NULL || ct + params->nbf_tolerance_seconds < strtoll(c->value, NULL, 10))
{
validation_res |= (unsigned)L8W8JWT_NBF_FAILURE;
@ -717,7 +711,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_iat)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "iat", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "iat", 3);
if (c == NULL || ct + params->iat_tolerance_seconds < strtoll(c->value, NULL, 10))
{
validation_res |= (unsigned)L8W8JWT_IAT_FAILURE;
@ -726,7 +720,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
if (params->validate_typ)
{
struct l8w8jwt_claim* c = l8w8jwt_get_claim((l8w8jwt_claim*)claims.array, claims.length, "typ", 3);
struct l8w8jwt_claim* c = l8w8jwt_get_claim((struct l8w8jwt_claim*)claims.array, claims.length, "typ", 3);
if (c == NULL || l8w8jwt_strncmpic(c->value, params->validate_typ, params->validate_typ_length) != 0)
{
validation_res |= (unsigned)L8W8JWT_TYP_FAILURE;
@ -734,7 +728,7 @@ int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validati
}
r = L8W8JWT_SUCCESS;
*out_validation_result = (l8w8jwt_validation_result)validation_res;
*out_validation_result = (enum l8w8jwt_validation_result)validation_res;
if (out_claims != NULL && out_claims_length != NULL)
{
@ -774,6 +768,6 @@ exit:
#undef JSMN_STATIC
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -14,9 +14,9 @@
limitations under the License.
*/
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "include/util.h"
#include "include/encode.h"
@ -683,6 +683,6 @@ exit:
return r;
}
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -26,9 +26,9 @@
#ifndef L8W8JWT_BASE64_H
#define L8W8JWT_BASE64_H
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "version.h"
#include <stdint.h>
@ -70,8 +70,8 @@ L8W8JWT_API int l8w8jwt_base64_encode(int url, const uint8_t* data, size_t data_
*/
L8W8JWT_API int l8w8jwt_base64_decode(int url, const char* data, size_t data_length, uint8_t** out, size_t* out_length);
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif // L8W8JWT_BASE64_H

View File

@ -23,9 +23,9 @@
#ifndef L8W8JWT_CLAIM_H
#define L8W8JWT_CLAIM_H
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "version.h"
#include <stdlib.h>
@ -139,8 +139,8 @@ L8W8JWT_API int l8w8jwt_write_claims(struct chillbuff* stringbuilder, struct l8w
*/
L8W8JWT_API struct l8w8jwt_claim* l8w8jwt_get_claim(struct l8w8jwt_claim* claims, size_t claims_count, const char* key, size_t key_length);
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif // L8W8JWT_CLAIM_H

View File

@ -23,9 +23,9 @@
#ifndef L8W8JWT_DECODE_H
#define L8W8JWT_DECODE_H
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "algs.h"
#include "claim.h"
@ -267,8 +267,8 @@ L8W8JWT_API int l8w8jwt_validate_decoding_params(struct l8w8jwt_decoding_params*
*/
L8W8JWT_API int l8w8jwt_decode(struct l8w8jwt_decoding_params* params, enum l8w8jwt_validation_result* out_validation_result, struct l8w8jwt_claim** out_claims, size_t* out_claims_length);
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif // L8W8JWT_DECODE_H

View File

@ -23,9 +23,9 @@
#ifndef L8W8JWT_ENCODE_H
#define L8W8JWT_ENCODE_H
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "algs.h"
#include "claim.h"
@ -199,8 +199,8 @@ L8W8JWT_API int l8w8jwt_validate_encoding_params(struct l8w8jwt_encoding_params*
*/
L8W8JWT_API int l8w8jwt_encode(struct l8w8jwt_encoding_params* params);
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif // L8W8JWT_ENCODE_H

View File

@ -23,9 +23,9 @@
#ifndef L8W8JWT_RETCODES_H
#define L8W8JWT_RETCODES_H
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* Returned from a l8w8jwt function when everything went smooth 'n' chill. Time to get Schwifty, Morteyy!
@ -100,8 +100,8 @@
*/
#define L8W8JWT_UNSUPPORTED_ALG 800
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif // L8W8JWT_RETCODES_H

View File

@ -23,9 +23,9 @@
#ifndef L8W8JWT_UTIL_H
#define L8W8JWT_UTIL_H
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include "version.h"
@ -51,8 +51,8 @@ L8W8JWT_API int l8w8jwt_hexstr2bin(const char* hexstr, size_t hexstr_length, uns
*/
L8W8JWT_API int l8w8jwt_strncmpic(const char* str1, const char* str2, size_t n);
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif // L8W8JWT_UTIL_H

View File

@ -23,9 +23,9 @@
#ifndef L8W8JWT_VERSION_H
#define L8W8JWT_VERSION_H
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* Current l8w8jwt version number.
@ -80,8 +80,8 @@ L8W8JWT_API int l8w8jwt_get_version_number(void);
*/
L8W8JWT_API void l8w8jwt_get_version_string(char out[32]);
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif // L8W8JWT_VERSION_H

View File

@ -17,9 +17,9 @@
#include "include/util.h"
#include <ctype.h>
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
int l8w8jwt_hexstr2bin(const char* hexstr, const size_t hexstr_length, unsigned char* output, const size_t output_size, size_t* output_length)
{
@ -82,6 +82,6 @@ int l8w8jwt_strncmpic(const char* str1, const char* str2, size_t n)
return ret;
}
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -22,9 +22,9 @@
#include <windows.h>
#endif
//#ifdef __cplusplus
//extern "C" {
//#endif
#ifdef __cplusplus
extern "C" {
#endif
void l8w8jwt_free(void* mem)
{
@ -70,6 +70,6 @@ void l8w8jwt_get_version_string(char out[32])
out[version_string_length] = '\0';
}
//#ifdef __cplusplus
//} // extern "C"
//#endif
#ifdef __cplusplus
} // extern "C"
#endif