Check if DCM feature is supported on the device 91/291891/3
authorDariusz Michaluk <d.michaluk@samsung.com>
Mon, 24 Apr 2023 08:40:45 +0000 (10:40 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 25 Apr 2023 15:49:15 +0000 (17:49 +0200)
Change-Id: I8cf77f3b7f48f2bc8977f81bc723cf363e831c2c

packaging/device-certificate-manager.spec
src/dcm-client/CMakeLists.txt
src/dcm-client/device_certificate_manager.cpp
src/include/device_certificate_manager.h
tests/CMakeLists.txt

index 585d10d..1faa7f8 100644 (file)
@@ -17,6 +17,7 @@ BuildRequires: pkgconfig(cynara-client)
 BuildRequires: pkgconfig(cynara-creds-socket)
 BuildRequires: pkgconfig(cynara-session)
 BuildRequires: pkgconfig(openssl1.1)
+BuildRequires: pkgconfig(capi-system-info)
 BuildRequires: boost-devel
 %if "%{build_type}" == "COVERAGE"
 BuildRequires: lcov
index 116fd3f..cc3ad9f 100644 (file)
@@ -23,7 +23,7 @@ FIND_PACKAGE(Boost REQUIRED
        COMPONENTS
        system)
 
-PKG_CHECK_MODULES(CLIENT_DEPS REQUIRED dlog)
+PKG_CHECK_MODULES(CLIENT_DEPS REQUIRED dlog capi-system-info)
 
 INCLUDE_DIRECTORIES(SYSTEM ${CLIENT_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
 LINK_DIRECTORIES(${CLIENT_DEPS_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS})
index ea46dc3..c4755d0 100644 (file)
@@ -20,6 +20,9 @@
 #include <cstring>
 #include <cstdlib>
 #include <cassert>
+#include <mutex>
+
+#include <system_info.h>
 
 #include "device_certificate_manager.h"
 #include "dcm_client.h"
 #define API_DEVICE_CERTIFICATE_MANAGER_EXPORT __attribute__((visibility("default")))
 #endif
 
+std::once_flag check_flag;
+static int check_result;
+
+void check_feature(void)
+{
+       bool is_supported;
+       if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(
+               "http://tizen.org/feature/security.device_certificate", &is_supported))
+               check_result = DCM_ERROR_UNKNOWN;
+       else if (!is_supported)
+               check_result = DCM_ERROR_NOT_SUPPORTED;
+       else
+               check_result = DCM_ERROR_NONE;
+}
+
+#define CHECK_FEATURE_SUPPORTED \
+       do { \
+               try { \
+                       std::call_once(check_flag, check_feature); \
+                       if (check_result != DCM_ERROR_NONE) \
+                               return check_result; \
+               } catch (...) { \
+                       return DCM_ERROR_UNKNOWN; \
+               } \
+       } while (0)
+
 struct dcm_key_context_internal {
        std::shared_ptr<dcm_client_connection> connection;
 };
@@ -37,6 +66,8 @@ struct dcm_key_context_internal {
 API_DEVICE_CERTIFICATE_MANAGER_EXPORT
 int dcm_create_key_context(const char *service, const char *usage, const char *key_type, void **key_ctx)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        try {
                if(!key_ctx)
                        return DCM_ERROR_INVALID_PARAMETER;
@@ -70,6 +101,8 @@ int dcm_create_key_context(const char *service, const char *usage, const char *k
 API_DEVICE_CERTIFICATE_MANAGER_EXPORT
 int dcm_free_key_context(void *key_ctx)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if(!key_ctx)
                return DCM_ERROR_NONE;
 
@@ -81,6 +114,8 @@ int dcm_free_key_context(void *key_ctx)
 API_DEVICE_CERTIFICATE_MANAGER_EXPORT
 int dcm_get_certificate_chain(const void *key_ctx, char **cert_chain, size_t *cert_chain_len)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if(!key_ctx || !cert_chain || !cert_chain_len)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -103,6 +138,8 @@ int dcm_get_certificate_chain(const void *key_ctx, char **cert_chain, size_t *ce
 API_DEVICE_CERTIFICATE_MANAGER_EXPORT
 int dcm_get_key_bit_length(const void *key_ctx, size_t *key_bit_len)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if(!key_ctx || !key_bit_len)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -116,6 +153,8 @@ int dcm_get_key_bit_length(const void *key_ctx, size_t *key_bit_len)
 API_DEVICE_CERTIFICATE_MANAGER_EXPORT
 int dcm_get_key_type(const void *key_ctx, char **key_type)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if(!key_ctx || !key_type)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -136,6 +175,8 @@ int dcm_create_signature(const void *key_ctx, dcm_digest_algorithm_e md,
        const char *message, size_t message_len,
        char **signature, size_t *signature_len)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if(!key_ctx || !signature || !signature_len)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -172,6 +213,8 @@ int dcm_e2ee_create_bundle(unsigned char *message,
        size_t message_len,
        dcm_e2ee_bundle_h* bundle)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if (message == NULL || bundle == NULL)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -209,6 +252,8 @@ int dcm_e2ee_get_bundle_message(const dcm_e2ee_bundle_h bundle,
        const unsigned char** message,
        size_t* message_len)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if (bundle == NULL || message == NULL || message_len == NULL)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -222,6 +267,8 @@ int dcm_e2ee_get_bundle_message(const dcm_e2ee_bundle_h bundle,
 API_DEVICE_CERTIFICATE_MANAGER_EXPORT
 int dcm_e2ee_get_bundle_platform(const dcm_e2ee_bundle_h bundle, const char** platform)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if (bundle == NULL || platform == NULL)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -234,6 +281,8 @@ int dcm_e2ee_get_bundle_platform(const dcm_e2ee_bundle_h bundle, const char** pl
 API_DEVICE_CERTIFICATE_MANAGER_EXPORT
 int dcm_e2ee_get_bundle_pkg_id(const dcm_e2ee_bundle_h bundle, const char** pkg_id)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if (bundle == NULL || pkg_id == NULL)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -248,6 +297,8 @@ int dcm_e2ee_get_bundle_payload(const dcm_e2ee_bundle_h bundle,
        const unsigned char** payload,
        size_t* payload_len)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if (bundle == NULL || payload == NULL || payload_len == NULL)
                return DCM_ERROR_INVALID_PARAMETER;
 
@@ -267,6 +318,8 @@ int dcm_e2ee_create_signed_bundle(const void *key_ctx,
        char **signature,
        size_t *signature_len)
 {
+       CHECK_FEATURE_SUPPORTED;
+
        if (key_ctx == NULL || payload == NULL || bundle == NULL || signature == NULL ||
                signature_len == NULL)
                return DCM_ERROR_INVALID_PARAMETER;
index a85c8d1..8718bfa 100644 (file)
@@ -250,9 +250,11 @@ typedef struct dcm_e2ee_bundle_s* dcm_e2ee_bundle_h;
  * @return #DCM_ERROR_NONE on success, otherwise a negative error value
  *
  * @retval #DCM_ERROR_NONE Successful
+ * @retval #DCM_ERROR_NOT_SUPPORTED Feature needed to run API is not supported
  * @retval #DCM_ERROR_INVALID_PARAMETER Input parameter is invalid (message = NULL or bundle = NULL)
  *                                      or the message format is invalid.
  * @retval #DCM_ERROR_MSG_FORMAT The message is not in Tizen platform format.
+ * @retval #DCM_ERROR_UNKNOWN Unknown error
  *
  * @see dcm_e2ee_free_bundle()
  * @see #dcm_e2ee_bundle_h
@@ -287,7 +289,9 @@ void dcm_e2ee_free_bundle(dcm_e2ee_bundle_h bundle);
  * @return #DCM_ERROR_NONE on success, otherwise a negative error value
  *
  * @retval #DCM_ERROR_NONE Successful
+ * @retval #DCM_ERROR_NOT_SUPPORTED Feature needed to run API is not supported
  * @retval #DCM_ERROR_INVALID_PARAMETER @a bundle, @a message or @a message_len is NULL.
+ * @retval #DCM_ERROR_UNKNOWN Unknown error
  *
  * @see dcm_e2ee_create_signed_bundle()
  * @see dcm_e2ee_free_bundle()
@@ -311,7 +315,9 @@ int dcm_e2ee_get_bundle_message(const dcm_e2ee_bundle_h bundle,
  * @return #DCM_ERROR_NONE on success, otherwise a negative error value
  *
  * @retval #DCM_ERROR_NONE Successful
+ * @retval #DCM_ERROR_NOT_SUPPORTED Feature needed to run API is not supported
  * @retval #DCM_ERROR_INVALID_PARAMETER @a bundle or @a platform is NULL.
+ * @retval #DCM_ERROR_UNKNOWN Unknown error
  *
  * @see dcm_e2ee_create_bundle()
  * @see dcm_e2ee_free_bundle()
@@ -333,7 +339,9 @@ int dcm_e2ee_get_bundle_platform(const dcm_e2ee_bundle_h bundle, const char** pl
  * @return #DCM_ERROR_NONE on success, otherwise a negative error value
  *
  * @retval #DCM_ERROR_NONE Successful
+ * @retval #DCM_ERROR_NOT_SUPPORTED Feature needed to run API is not supported
  * @retval #DCM_ERROR_INVALID_PARAMETER @a bundle or @a pkg_id is NULL.
+ * @retval #DCM_ERROR_UNKNOWN Unknown error
  *
  * @see dcm_e2ee_create_bundle()
  * @see dcm_e2ee_free_bundle()
@@ -357,7 +365,9 @@ int dcm_e2ee_get_bundle_pkg_id(const dcm_e2ee_bundle_h bundle, const char** pkg_
  * @return #DCM_ERROR_NONE on success, otherwise a negative error value
  *
  * @retval #DCM_ERROR_NONE Successful
+ * @retval #DCM_ERROR_NOT_SUPPORTED Feature needed to run API is not supported
  * @retval #DCM_ERROR_INVALID_PARAMETER @a bundle, @a payload or @a payload_len is NULL.
+ * @retval #DCM_ERROR_UNKNOWN Unknown error
  *
  * @see dcm_e2ee_create_bundle()
  * @see dcm_e2ee_free_bundle()
index 075a757..81a13de 100644 (file)
@@ -48,7 +48,7 @@ FIND_PACKAGE(Boost REQUIRED
        COMPONENTS
        unit_test_framework)
 
-PKG_CHECK_MODULES(TEST_DEPS REQUIRED dlog libsmack)
+PKG_CHECK_MODULES(TEST_DEPS REQUIRED dlog libsmack capi-system-info)
 
 INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIRS} ${TEST_DEPS_INCLUDE_DIRS})
 LINK_DIRECTORIES(${Boost_LIBRARY_DIRS} ${TEST_DEPS_LIBRARY_DIRS})