diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8005f77 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,213 @@ +cmake_minimum_required (VERSION 3.0.2) +project (cjose) + +include(CheckSymbolExists) +include(CheckIncludeFile) +include(CheckTypeSize) + +option(ENABLE_Tests "Turn on Tests" ON) +option(CJOSE_BUILD_SHARED_LIBS "Build shared libraries." OFF) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMakeModules") + +# Build Release by default +if (NOT CMAKE_BUILD_TYPE) +set(CMAKE_BUILD_TYPE Release) +else() +endif() + +# Setup cjose version +set(CJOSE_MAJOR_VERSION 0) +set(CJOSE_MINOR_VERSION 4) +set(CJOSE_PATCH_VERSION 1) +set(PACKAGE_VERSION ${CJOSE_MAJOR_VERSION}.${CJOSE_MINOR_VERSION}.${CJOSE_PATCH_VERSION}) + +# Set output directories +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) + +# Create version.h file +configure_file ( + "${PROJECT_SOURCE_DIR}/include/cjose/version.h.in" + "${PROJECT_BINARY_DIR}/include/cjose/version.h" + ) + +# Set include directories +include_directories( +${CMAKE_CURRENT_SOURCE_DIR}/include +${CMAKE_CURRENT_SOURCE_DIR}/src/include +${CMAKE_CURRENT_SOURCE_DIR}/src +) + +# Find dependencies +# Look for Homebrew version of OpenSSL +function(GetRootDirForHomebrew PKG_NAME) + if ((NOT ${PKG_NAME}_INCLUDE_DIR) OR (NOT ${PKG_NAME}_LIBRARIES)) + string(TOLOWER ${PKG_NAME} PKG_NAME_LOWER) + file(GLOB HB_${PKG_NAME}_ROOT_DIRECTORIES /usr/local/Cellar/${PKG_NAME_LOWER}/*) + list(GET HB_${PKG_NAME}_ROOT_DIRECTORIES 0 HB_${PKG_NAME}_ROOT_DIR) + if(HB_${PKG_NAME}_ROOT_DIRECTORIES AND NOT ${PKG_NAME}_ROOT_DIR) + SET(${PKG_NAME}_ROOT_DIR ${HB_${PKG_NAME}_ROOT_DIR} PARENT_SCOPE) + endif(HB_${PKG_NAME}_ROOT_DIRECTORIES AND NOT ${PKG_NAME}_ROOT_DIR) + endif((NOT ${PKG_NAME}_INCLUDE_DIR) OR (NOT ${PKG_NAME}_LIBRARIES)) +endfunction(GetRootDirForHomebrew PKG_NAME) + +if(CMAKE_C_COMPILER_ID MATCHES "AppleClang") + GetRootDirForHomebrew(OPENSSL) + GetRootDirForHomebrew(CHECK) + GetRootDirForHomebrew(JANSSON) +endif(CMAKE_C_COMPILER_ID MATCHES "AppleClang") + +find_package(OpenSSL 1.0.1 REQUIRED) +find_package(Jansson 2.3 REQUIRED) +find_package(Check 0.9.4) + +find_library(RT NAMES rt) +if (${RT} STREQUAL RT-NOTFOUND) + unset(${RT}) +endif(${RT} STREQUAL RT-NOTFOUND) + +find_library(M NAMES m) +if (${M} STREQUAL M-NOTFOUND) + unset(${M}) +endif(${M} STREQUAL M-NOTFOUND) + +check_type_size(ssize_t SSIZE_T) + +# Enable test if possible +if (NOT CHECK_FOUND OR ENABLE_Tests==OFF) + SET(ENABLE_Tests OFF) +endif(NOT CHECK_FOUND OR ENABLE_Tests==OFF) +message(STATUS "Tests Enabled............${ENABLE_Tests}") + +CHECK_SYMBOL_EXISTS("random" stdlib.h HAVE_RANDOM) + +# Set dependency include directories +include_directories( +${OPENSSL_INCLUDE_DIR} +${JANSSON_INCLUDE_DIRS} +) + +#Generate config file +configure_file ( + "${PROJECT_SOURCE_DIR}/include/cjose/config.h.in" + "${PROJECT_BINARY_DIR}/include/cjose/cjose_config.h" +) + +# Setup compilation flags for different compilers +if (MSVC) + add_compile_options(/W2 /nologo /WX-) + add_definitions( "/DVERSION=\"${PACKAGE_VERSION}\"") + add_definitions( "/D_CRT_SECURE_NO_WARNINGS") + set(DEFAULT_COMPILER_OPTIONS /Zi /DEBUG $<$:/Od> $<$:/O2> /MP ) + set(LIB_COMPILER_OPTIONS ${DEFAULT_COMPILER_OPTIONS} /WX) + if (STATIC_CRT) + SET(LIB_COMPILER_OPTIONS ${LIB_COMPILER_OPTIONS} $<$:/MTd> $<$:/MT>) + else() + SET(LIB_COMPILER_OPTIONS ${LIB_COMPILER_OPTIONS} $<$:/MDd> $<$:/MD>) + endif() +elseif(CMAKE_COMPILER_IS_GNUCC) + add_definitions( "-DVERSION=\"${PACKAGE_VERSION}\"") + set(DEFAULT_COMPILER_OPTIONS -std=gnu99 -g $<$:-O2>) + set(LIB_COMPILER_OPTIONS ${DEFAULT_COMPILER_OPTIONS} --pedantic -Wall -Werror) +else(CMAKE_C_COMPILER_ID MATCHES "AppleClang") + add_definitions( "-DVERSION=\"${PACKAGE_VERSION}\"") + set(DEFAULT_COMPILER_OPTIONS -std=gnu99 -g $<$:-O2>) + set(LIB_COMPILER_OPTIONS ${DEFAULT_COMPILER_OPTIONS} --pedantic -Wall -Werror) +endif(MSVC) + + +file(GLOB CJOSE_SRC ${PROJECT_SOURCE_DIR}/src/*.c) +file(GLOB CJOSE_HDR_PUBLIC ${PROJECT_SOURCE_DIR}/include/cjose/*.h) +file(GLOB CJOSE_HDR_PRIVATE ${PROJECT_SOURCE_DIR}/src/include/*.h) + +source_group("Library Sources" FILES ${CJOSE_SRC}) +source_group("Library Private Headers" FILES ${CJOSE_HDR_PRIVATE}) +source_group("Library Public Headers" FILES ${CJOSE_HDR_PUBLIC}) + +if (CJOSE_BUILD_SHARED_LIBS) +add_library(cjose SHARED +${CJOSE_SRC} +${CJOSE_HDR_PRIVATE} +${CJOSE_HDR_PUBLIC} +) +else() +add_library(cjose +${CJOSE_SRC} +${CJOSE_HDR_PRIVATE} +${CJOSE_HDR_PUBLIC} +) +endif() + +target_link_libraries(cjose ${JANSSON_LIBRARIES} ${OPENSSL_LIBRARIES} ${MLIB} ${RTLIB}) +target_compile_options(cjose PRIVATE ${LIB_COMPILER_OPTIONS}) + +if (ENABLE_Tests) +file(GLOB CJOSE_TESTS_SRC ${PROJECT_SOURCE_DIR}/test/*.c) +file(GLOB CJOSE_TESTS_HDR ${PROJECT_SOURCE_DIR}/test/*.h) +include_directories(${CHECK_INCLUDE_DIRS}) +source_group("Test Files " FILES ${CJOSE_TESTS_SRC} ${CJOSE_TESTS_HDR}) + +add_executable(tests ${CJOSE_TESTS_SRC} ${CJOSE_TESTS_HDR}) +target_compile_options(tests PRIVATE ${DEFAULT_COMPILER_OPTIONS}) +target_link_libraries(tests cjose ${JANSSON_LIBRARIES} ${OPENSSL_LIBRARIES} ${CHECK_LIBRARIES}) +endif(ENABLE_Tests) + +#install + +# Allow the user to override installation directories. +set(CJOSE_INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries") +set(CJOSE_INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables") +set(CJOSE_INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files") + + +if(WIN32 AND NOT CYGWIN) + set(DEF_INSTALL_CMAKE_DIR cmake) +else() + set(DEF_INSTALL_CMAKE_DIR lib/cmake/cjose) +endif() + +set(CJOSE_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files") + +# Create pkg-conf file. +# (We use the same files as ./configure does, so we +# have to defined the same variables used there). +set(prefix ${CMAKE_INSTALL_PREFIX}) +set(exec_prefix ${CMAKE_INSTALL_PREFIX}) +set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}) +set(includedir ${CMAKE_INSTALL_PREFIX}/${CJOSE_INSTALL_INCLUDE_DIR}) +set(VERSION ${PACKAGE_VERSION}) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cjose.pc.in + ${CMAKE_CURRENT_BINARY_DIR}/cjose.pc @ONLY) + +# Make sure the paths are absolute. +foreach(p LIB BIN INCLUDE CMAKE) + set(var CJOSE_INSTALL_${p}_DIR) + if(NOT IS_ABSOLUTE "${${var}}") + set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}") + endif() +endforeach() + +set_target_properties(cjose PROPERTIES PUBLIC_HEADER "${CJOSE_HDR_PUBLIC}") + + +# Install the pkg-config. +install (FILES + ${CMAKE_CURRENT_BINARY_DIR}/cjose.pc + DESTINATION ${CJOSE_INSTALL_LIB_DIR}/pkgconfig COMPONENT dev) + +install(TARGETS cjose + EXPORT CjoseTargets + LIBRARY DESTINATION "${CJOSE_INSTALL_LIB_DIR}" COMPONENT lib + ARCHIVE DESTINATION "${CJOSE_INSTALL_LIB_DIR}" COMPONENT lib + RUNTIME DESTINATION "${CJOSE_INSTALL_BIN_DIR}" COMPONENT lib # Windows DLLs + PUBLIC_HEADER DESTINATION "${CJOSE_INSTALL_INCLUDE_DIR}/cjose" COMPONENT dev) + +# Install exports for the install-tree. +install(EXPORT CjoseTargets + DESTINATION "${CJOSE_INSTALL_CMAKE_DIR}" COMPONENT dev) + +# For use when simply using add_library from a parent project to build cjose. +set(CJOSE_LIBRARIES cjose CACHE STRING "Cjose libraries") + diff --git a/CMakeModules/FindCheck.cmake b/CMakeModules/FindCheck.cmake new file mode 100644 index 0000000..d8f08f0 --- /dev/null +++ b/CMakeModules/FindCheck.cmake @@ -0,0 +1,74 @@ +if (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS) + set(CHECK_FOUND TRUE) +else (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS) + find_path(CHECK_INCLUDE_DIR + NAMES + check.h + PATHS + ${CHECK_ROOT_DIR}/include + NO_DEFAULT_PATH + ) + + find_path(CHECK_INCLUDE_DIR + NAMES + check.h + ) + +find_library(CHECK_LIBRARY_CHECK + NAMES + check + compat + PATHS + ${CHECK_ROOT_DIR}/lib + NO_DEFAULT_PATH + ) + +find_library(CHECK_LIBRARY_CHECK + NAMES + check + compat + ) + +find_library(CHECK_LIBRARY_COMPAT + NAMES + compat + PATHS + ${CHECK_ROOT_DIR}/lib + NO_DEFAULT_PATH + ) + +find_library(CHECK_LIBRARY_COMPAT + NAMES + compat + ) + +if (CHECK_INCLUDE_DIR) +set(CHECK_INCLUDE_DIRS + ${CHECK_INCLUDE_DIRS} + ${CHECK_INCLUDE_DIR} + ) +endif(CHECK_INCLUDE_DIR) + +if (CHECK_LIBRARY_CHECK) + set(CHECK_LIBRARIES + ${CHECK_LIBRARIES} + ${CHECK_LIBRARY_CHECK} + ) +endif (CHECK_LIBRARY_CHECK) + +if (CHECK_LIBRARY_COMPAT) + set(CHECK_LIBRARIES + ${CHECK_LIBRARIES} + ${CHECK_LIBRARY_COMPAT} + ) +endif (CHECK_LIBRARY_COMPAT) + + include(FindPackageHandleStandardArgs) + find_package_handle_standard_args(Check DEFAULT_MSG + CHECK_LIBRARIES CHECK_INCLUDE_DIRS) + + mark_as_advanced(CHECK_INCLUDE_DIRS CHECK_LIBRARIES) + +endif (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS) + + diff --git a/CMakeModules/FindJansson.cmake b/CMakeModules/FindJansson.cmake new file mode 100644 index 0000000..2f3eac1 --- /dev/null +++ b/CMakeModules/FindJansson.cmake @@ -0,0 +1,70 @@ +if (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS) + set(JANSSON_FOUND TRUE) +else (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS) + find_path(JANSSON_INCLUDE_DIR + NAMES + jansson.h + PATHS + ${JANSSON_ROOT_DIR}/include + NO_DEFAULT_PATH + ) + + find_path(JANSSON_INCLUDE_DIR + NAMES + jansson.h + ) + +find_library(JANSSON_LIBRARY_RELEASE + NAMES + jansson + PATHS + ${JANSSON_ROOT_DIR}/lib + NO_DEFAULT_PATH + ) + +find_library(JANSSON_LIBRARY_RELEASE + NAMES + jansson + ) + +find_library(JANSSON_LIBRARY_DEBUG + NAMES + jansson_d + PATHS + ${JANSSON_ROOT_DIR}/lib + NO_DEFAULT_PATH + ) + +find_library(JANSSON_LIBRARY_DEBUG + NAMES + jansson_d + ) + +set(JANSSON_INCLUDE_DIRS + ${JANSSON_INCLUDE_DIR} + ) + +if (JANSSON_LIBRARY_DEBUG) + set(JANSSON_LIBRARIES + ${JANSSON_LIBRARIES} + debug ${JANSSON_LIBRARY_DEBUG} + ) +endif (JANSSON_LIBRARY_DEBUG) + +if (JANSSON_LIBRARY_RELEASE) + if (WIN32) + set(JANSSON_LIBRARIES ${JANSSON_LIBRARIES} optimized ${JANSSON_LIBRARY_RELEASE}) + else() + set(JANSSON_LIBRARIES ${JANSSON_LIBRARIES} general ${JANSSON_LIBRARY_RELEASE}) + endif(WIN32) +endif (JANSSON_LIBRARY_RELEASE) + + include(FindPackageHandleStandardArgs) + find_package_handle_standard_args(Jansson DEFAULT_MSG + JANSSON_LIBRARIES JANSSON_INCLUDE_DIRS) + + mark_as_advanced(JANSSON_INCLUDE_DIRS JANSSON_LIBRARIES) + +endif (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS) + + diff --git a/include/cjose/base64.h b/include/cjose/base64.h index b594f8d..3164984 100644 --- a/include/cjose/base64.h +++ b/include/cjose/base64.h @@ -37,7 +37,7 @@ extern "C" * \param err [out] An optional error object which can be used to get additional * information in the event of an error. */ -bool cjose_base64_encode(const uint8_t *input, const size_t inlen, char **output, size_t *outlen, cjose_err *err); +bool cjose_base64_encode(const uint8_t *input, size_t inlen, char **output, size_t *outlen, cjose_err *err); /** * Encodes the given octet string to URL-safe Base64. * @@ -49,7 +49,7 @@ bool cjose_base64_encode(const uint8_t *input, const size_t inlen, char **output * \param err [out] An optional error object which can be used to get additional * information in the event of an error. */ -bool cjose_base64url_encode(const uint8_t *input, const size_t inlen, char **output, size_t *outlen, cjose_err *err); +bool cjose_base64url_encode(const uint8_t *input, size_t inlen, char **output, size_t *outlen, cjose_err *err); /** * Decodes the given string from Base64. @@ -63,7 +63,7 @@ bool cjose_base64url_encode(const uint8_t *input, const size_t inlen, char **out * \param err [out] An optional error object which can be used to get additional * information in the event of an error. */ -bool cjose_base64_decode(const char *input, const size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err); +bool cjose_base64_decode(const char *input, size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err); /** * Decodes the given string from URL-Safe Base64. * @@ -76,7 +76,7 @@ bool cjose_base64_decode(const char *input, const size_t inlen, uint8_t **output * \param err [out] An optional error object which can be used to get additional * information in the event of an error. */ -bool cjose_base64url_decode(const char *input, const size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err); +bool cjose_base64url_decode(const char *input, size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err); #ifdef __cplusplus diff --git a/include/cjose/config.h.in b/include/cjose/config.h.in new file mode 100644 index 0000000..a2d004f --- /dev/null +++ b/include/cjose/config.h.in @@ -0,0 +1,14 @@ +#pragma once +#ifndef CONFIG_H +#define CONFIG_H + +#cmakedefine HAVE_RANDOM + +#cmakedefine HAVE_SSIZE_T + +#ifndef HAVE_SSIZE_T +#undef ssize_t +typedef int ssize_t; +#endif + +#endif diff --git a/lib b/lib deleted file mode 120000 index df64832..0000000 --- a/lib +++ /dev/null @@ -1 +0,0 @@ -src/.libs/ \ No newline at end of file diff --git a/src/include/util_int.h b/src/include/util_int.h index c8d059c..c4ad01b 100644 --- a/src/include/util_int.h +++ b/src/include/util_int.h @@ -9,6 +9,7 @@ #define SRC_UTIL_INT_H #include +#include #include #include diff --git a/src/jwk.c b/src/jwk.c index b3a9777..a406d55 100644 --- a/src/jwk.c +++ b/src/jwk.c @@ -1357,7 +1357,7 @@ static bool _decode_json_object_base64url_attribute(json_t *jwk_json, const char *end = NULL; for (end = str + strlen(str) - 1; *end == '=' && end > str; --end); size_t unpadded_len = end + 1 - str - ((*end == '=') ? 1 : 0); - size_t expected_len = ceil(4 * ((float)*buflen / 3)); + size_t expected_len = (size_t)(ceil(4 * ((float)*buflen / 3))); if (expected_len != unpadded_len) { diff --git a/test/tests_util.c b/test/tests_util.c new file mode 100644 index 0000000..75231af --- /dev/null +++ b/test/tests_util.c @@ -0,0 +1,9 @@ +#include "tests_util.h" +#include + +#ifndef HAVE_RANDOM +int random(void) +{ + return rand(); +} +#endif diff --git a/test/tests_util.h b/test/tests_util.h new file mode 100644 index 0000000..805b955 --- /dev/null +++ b/test/tests_util.h @@ -0,0 +1,15 @@ +#ifndef TEST_UTIL_C +#define TEST_UTIL_C + +#include "cjose/cjose_config.h" + +#ifndef HAVE_RANDOM +/** + * Generate random + * + * \returns Random number from 0 to RAND_MAX + */ +int random(void); + +#endif +#endif