jwt.c File Reference

JWT module implementation file. More...

#include "jwt.h"
#include "time_util.h"
#include "json_util.h"
#include "definitions.h"
#include "string_util.h"
#include "mcl_core/mcl_assert.h"
#include "mcl_core/mcl_memory.h"
Include dependency graph for jwt.c:

Go to the source code of this file.

Macros

#define JWT_HEADER_NAME_ALG   "alg"
 
#define JWT_HEADER_NAME_TYPE   "typ"
 
#define JWT_HEADER_NAME_KID   "kid"
 
#define JWT_PAYLOAD_NAME_ISS   "iss"
 
#define JWT_PAYLOAD_NAME_SUB   "sub"
 
#define JWT_PAYLOAD_NAME_AUD   "aud"
 
#define JWT_PAYLOAD_NAME_IAT   "iat"
 
#define JWT_PAYLOAD_NAME_NBF   "nbf"
 
#define JWT_PAYLOAD_NAME_EXP   "exp"
 
#define JWT_PAYLOAD_NAME_SCHEMAS   "schemas"
 
#define JWT_PAYLOAD_NAME_TEN   "ten"
 
#define JWT_PAYLOAD_NAME_JTI   "jti"
 
#define JWT_PAYLOAD_NAME_PUBLIC_KEY   "public_key"
 
#define JWT_PAYLOAD_NAME_CONTENT_MD5   "content-md5"
 
#define JWT_HEADER_VALUE_ALG_SHARED_SECRET   "HS256"
 
#define JWT_HEADER_VALUE_ALG_RSA_3072   "RS256"
 
#define JWT_HEADER_VALUE_TYP   "JWT"
 
#define JWT_PAYLOAD_VALUE_SCHEMAS   "urn:siemens:mindsphere:v1"
 
#define JWT_PAYLOAD_VALUE_AUD   "southgate"
 

Functions

static mcl_error_t _create_self_issued_jwt_header (E_MCL_SECURITY_PROFILE security_profile, mcl_json_t **header)
 
static mcl_error_t _create_self_issued_jwt_payload (security_handler_t *security_handler, char *tenant, jwt_t *jwt)
 
static mcl_error_t _add_schema_to_jwt (mcl_json_t *payload)
 
static mcl_error_t _get_header_and_payload_encoded_base64_url (char *header, char *payload, char **header_encoded, char **payload_encoded)
 
static mcl_error_t _join_with_dot (char *string_a, char *string_b, char **joined)
 
static mcl_error_t _calculate_signature (jwt_t *jwt, char *header_and_payload, char **signature)
 
static mcl_error_t _generate_token (jwt_t *jwt, char *header, char *payload, char **token)
 
mcl_error_t jwt_initialize (security_handler_t *security_handler, E_MCL_SECURITY_PROFILE security_profile, char *tenant, jwt_t **jwt)
 
char * jwt_get_token (jwt_t *jwt)
 
void jwt_destroy (jwt_t **jwt)
 

Detailed Description

JWT module implementation file.

Definition in file jwt.c.

Macro Definition Documentation

#define JWT_HEADER_NAME_ALG   "alg"

Definition at line 18 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_NAME_KID   "kid"

Definition at line 20 of file jwt.c.

#define JWT_HEADER_NAME_TYPE   "typ"

Definition at line 19 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_VALUE_ALG_RSA_3072   "RS256"

Definition at line 35 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_VALUE_ALG_SHARED_SECRET   "HS256"

Definition at line 34 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_VALUE_TYP   "JWT"

Definition at line 36 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_PAYLOAD_NAME_AUD   "aud"

Definition at line 24 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_CONTENT_MD5   "content-md5"

Definition at line 32 of file jwt.c.

#define JWT_PAYLOAD_NAME_EXP   "exp"

Definition at line 27 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_IAT   "iat"

Definition at line 25 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_ISS   "iss"

Definition at line 22 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_JTI   "jti"

Definition at line 30 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_NBF   "nbf"

Definition at line 26 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_PUBLIC_KEY   "public_key"

Definition at line 31 of file jwt.c.

#define JWT_PAYLOAD_NAME_SCHEMAS   "schemas"

Definition at line 28 of file jwt.c.

Referenced by _add_schema_to_jwt().

#define JWT_PAYLOAD_NAME_SUB   "sub"

Definition at line 23 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_TEN   "ten"

Definition at line 29 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_VALUE_AUD   "southgate"

Definition at line 39 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_VALUE_SCHEMAS   "urn:siemens:mindsphere:v1"

Definition at line 38 of file jwt.c.

Referenced by _add_schema_to_jwt().

Function Documentation

static mcl_error_t _add_schema_to_jwt ( mcl_json_t payload)
static

Definition at line 391 of file jwt.c.

References json_util_add_string(), json_util_start_array(), JWT_PAYLOAD_NAME_SCHEMAS, JWT_PAYLOAD_VALUE_SCHEMAS, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_NULL, and MCL_OK.

Referenced by _create_self_issued_jwt_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t _calculate_signature ( jwt_t jwt,
char *  header_and_payload,
char **  signature 
)
static
static mcl_error_t _create_self_issued_jwt_header ( E_MCL_SECURITY_PROFILE  security_profile,
mcl_json_t **  header 
)
static
static mcl_error_t _generate_token ( jwt_t jwt,
char *  header,
char *  payload,
char **  token 
)
static

Definition at line 274 of file jwt.c.

References _calculate_signature(), _get_header_and_payload_encoded_base64_url(), _join_with_dot(), MCL_ASSERT_CODE_MESSAGE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_ERROR_RETURN, MCL_FAIL, MCL_FREE, MCL_NULL, and MCL_OK.

Referenced by jwt_get_token().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t _get_header_and_payload_encoded_base64_url ( char *  header,
char *  payload,
char **  header_encoded,
char **  payload_encoded 
)
static

Definition at line 304 of file jwt.c.

References MCL_ASSERT_CODE_MESSAGE, MCL_ASSERT_STATEMENT_CODE_MESSAGE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_FREE, MCL_OK, security_handler_base64_url_encode(), and string_util_strlen().

Referenced by _generate_token().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t _join_with_dot ( char *  string_a,
char *  string_b,
char **  joined 
)
static

Definition at line 321 of file jwt.c.

References MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FREE, MCL_MALLOC, MCL_NULL, MCL_NULL_CHAR_SIZE, MCL_OK, MCL_OUT_OF_MEMORY, string_util_memcpy(), and string_util_strlen().

Referenced by _generate_token().

Here is the call graph for this function:

Here is the caller graph for this function:

void jwt_destroy ( jwt_t **  jwt)

This function destroys jwt_t data structure.

Parameters
[in]jwtJWT handle which is going to be destroyed.

Definition at line 124 of file jwt.c.

References json_util_destroy(), MCL_DEBUG, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FREE, and MCL_NULL.

Referenced by _compose_access_token_request_payload(), and jwt_initialize().

Here is the call graph for this function:

Here is the caller graph for this function:

char* jwt_get_token ( jwt_t jwt)

This function is used to generate the JWT as json string.

Parameters
[in]jwtJWT handle.
Returns
Pointer to the JWT of type char or NULL in case of an error.

Definition at line 90 of file jwt.c.

References _generate_token(), jwt_t::header, mcl_core_return_code_strings, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_ERROR, MCL_FREE, mcl_json_util_to_string(), MCL_NULL, MCL_OK, and jwt_t::payload.

Referenced by _compose_access_token_request_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

mcl_error_t jwt_initialize ( security_handler_t security_handler,
E_MCL_SECURITY_PROFILE  security_profile,
char *  tenant,
jwt_t **  jwt 
)

This function creates and initializes a data struct of jwt_t.

Parameters
[in]security_handlerAlready initialized security handler. All JWT operations will be done using this object.
[in]security_profileOnboarding security profile.
[in]tenantTenant which will be set in JWT payload.
[out]jwtThe newly initialized jwt handle.
Returns
  • MCL_OK in case of success.
  • MCL_OUT_OF_MEMORY in case there is not enough memory in the system to proceed.
  • MCL_FAIL in case JSON object can not be created.

Definition at line 62 of file jwt.c.

References _create_self_issued_jwt_header(), _create_self_issued_jwt_payload(), jwt_destroy(), MCL_ASSERT_CODE_MESSAGE, MCL_ASSERT_STATEMENT_CODE_MESSAGE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_NEW, MCL_NULL, MCL_OK, and MCL_OUT_OF_MEMORY.

Referenced by _compose_access_token_request_payload().

Here is the call graph for this function:

Here is the caller graph for this function: