diff options
author | youngman <yman.jung@samsung.com> | 2016-09-08 16:31:48 +0900 |
---|---|---|
committer | youngman <yman.jung@samsung.com> | 2016-11-01 17:02:25 +0900 |
commit | 6a99f52efc40f431cb0e006ddeceaa4f4485ebda (patch) | |
tree | b600c4c753c5cede97862087f3d3ab4126a55580 | |
parent | d1893c63648c4ede7bb9e9a976c4b740b771ee56 (diff) | |
download | iotcon-6a99f52efc40f431cb0e006ddeceaa4f4485ebda.tar.gz iotcon-6a99f52efc40f431cb0e006ddeceaa4f4485ebda.tar.bz2 iotcon-6a99f52efc40f431cb0e006ddeceaa4f4485ebda.zip |
Add randompin API for security
Change-Id: If75a6bd46d9030c0d33e72de9114aaa79793b446
Signed-off-by: youngman <yman.jung@samsung.com>
-rw-r--r-- | include/iotcon.h | 62 | ||||
-rw-r--r-- | src/ic-ioty.c | 86 | ||||
-rw-r--r-- | src/ic-ioty.h | 2 | ||||
-rw-r--r-- | src/ic.c | 30 |
4 files changed, 172 insertions, 8 deletions
diff --git a/include/iotcon.h b/include/iotcon.h index 738aa50..fb29fa7 100644 --- a/include/iotcon.h +++ b/include/iotcon.h @@ -195,6 +195,68 @@ int iotcon_polling_set_interval(int interval); int iotcon_polling_invoke(void); /** + * @brief Specifies the type of function passed to iotcon_add_generated_pin_cb(). + * @details When a provisioning tool calls the function for registering this device, + * it is called, immediately. + * + * @since_tizen 3.0 + * + * @param[in] pin The pin number which is generated automatically + * @param[in] user_data The user data to pass to the function + * + * @pre The callback must be registered using iotcon_add_generated_pin_cb() + * + * @see iotcon_add_generated_pin_cb() + * @see iotcon_remove_generated_pin_cb() + */ +typedef void (*iotcon_generated_pin_cb)(const char *pin, void *user_data); + +/** + * @brief Adds callback to show pin number which is generated automatically. + * @details When a provisioning tool tries to register this device using random pin based\n + * ownership transfer method, it makes pin number which is generated automatically be shown. + * + * @since_tizen 3.0 + * + * @param[in] cb The callback function to invoke + * @param[in] user_data The user data to pass to the function + * + * @return 0 on success, otherwise a negative error value. + * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_NOT_SUPPORTED Not supported + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #IOTCON_ERROR_ALREADY Already done + * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory + * + * @pre iotcon_initialize() should be called to initialize. + * @post When the device is registered by provisioning tool, iotcon_generated_pin_cb() will be called. + * + * @see iotcon_remove_generated_pin_cb() + */ +int iotcon_add_generated_pin_cb(iotcon_generated_pin_cb cb, void *user_data); + +/** + * @brief Removes callback to show pin number which is generated automatically. + * @details If this function is called, @a cb will be not called anymore.\n + * For removing @a cb that is used at iotcon_add_generated_pin_cb() should be used. + * + * @since_tizen 3.0 + * + * @param[in] cb The callback function to invoke + * + * @return 0 on success, otherwise a negative error value. + * @retval #IOTCON_ERROR_NONE Successful + * @retval #IOTCON_ERROR_NOT_SUPPORTED Not supported + * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #IOTCON_ERROR_NO_DATA No data available + * + * @pre iotcon_initialize() should be called to initialize. + * + * @see iotcon_add_generated_pin_cb() + */ +int iotcon_remove_generated_pin_cb(iotcon_generated_pin_cb cb); + +/** * @} */ diff --git a/src/ic-ioty.c b/src/ic-ioty.c index b0724d2..fdf0892 100644 --- a/src/ic-ioty.c +++ b/src/ic-ioty.c @@ -47,9 +47,16 @@ #include "ic-ioty-ocprocess.h" #include "ic-cbor.h" +typedef struct { + iotcon_generated_pin_cb cb; + void *user_data; +} icl_generated_pin_cb_container_s; + static bool icl_state; +static bool icl_is_set_generated_pin_cb; static char icl_svr_db_file[PATH_MAX]; static OCPersistentStorage icl_ioty_ps; +static GList *icl_generated_pin_cb_list; void icl_ioty_deinit(pthread_t thread) { @@ -131,18 +138,84 @@ int icl_ioty_set_persistent_storage(const char *file_path, bool is_pt) return IOTCON_ERROR_NONE; } -void show_pin(char *pin, size_t length) +static void _generated_pin_cb(char *pin, size_t length) { - FN_CALL; + GList *node; + icl_generated_pin_cb_container_s *container; + if (NULL == pin) { + ERR("Invalid Pin Number"); + return; + } INFO("PIN : %s", pin); + + for (node = icl_generated_pin_cb_list; node; node = node->next) { + container = node->data; + if (container->cb) + container->cb(pin, container->user_data); + } } -int icl_ioty_set_generate_pin_cb() +static icl_generated_pin_cb_container_s* _find_generated_pin_cb( + iotcon_generated_pin_cb cb) { - FN_CALL; + GList *node; + icl_generated_pin_cb_container_s *container; + + for (node = icl_generated_pin_cb_list; node; node = node->next) { + container = node->data; + if (cb == container->cb) + return container; + } + + return NULL; +} + +int icl_ioty_add_generated_pin_cb(iotcon_generated_pin_cb cb, void *user_data) +{ + icl_generated_pin_cb_container_s *container; + + if (_find_generated_pin_cb(cb)) { + ERR("This callback is already registered."); + return IOTCON_ERROR_ALREADY; + } + + container = calloc(1, sizeof(icl_generated_pin_cb_container_s)); + if (NULL == container) { + ERR("calloc() Fail(%d)", errno); + return IOTCON_ERROR_OUT_OF_MEMORY; + } + container->cb = cb; + container->user_data = user_data; - SetGeneratePinCB(&show_pin); + icl_generated_pin_cb_list = g_list_append(icl_generated_pin_cb_list, container); + + if (false == icl_is_set_generated_pin_cb) { + SetGeneratePinCB(&_generated_pin_cb); + icl_is_set_generated_pin_cb = true; + } + + return IOTCON_ERROR_NONE; +} + +int icl_ioty_remove_generated_pin_cb(iotcon_generated_pin_cb cb) +{ + icl_generated_pin_cb_container_s *container; + + container = _find_generated_pin_cb(cb); + if (NULL == container) { + ERR("This callback is not registered"); + return IOTCON_ERROR_NO_DATA; + } + + if (1 == g_list_length(icl_generated_pin_cb_list)) { + ERR("It is required at least one function"); + return IOTCON_ERROR_INVALID_PARAMETER; + } + + icl_generated_pin_cb_list = g_list_remove(icl_generated_pin_cb_list, container); + + free(container); return IOTCON_ERROR_NONE; } @@ -180,9 +253,6 @@ int icl_ioty_init(pthread_t *out_thread) return ic_ioty_parse_oic_error(ret); } - // TODO: temp code - icl_ioty_set_generate_pin_cb(); - icl_ioty_ocprocess_start(); pthread_attr_init(&attr); diff --git a/src/ic-ioty.h b/src/ic-ioty.h index dbd02eb..182f9fd 100644 --- a/src/ic-ioty.h +++ b/src/ic-ioty.h @@ -46,6 +46,8 @@ void icl_ioty_deinit(pthread_t thread); int icl_ioty_init(pthread_t *out_thread); int icl_ioty_set_persistent_storage(const char *file_path, bool is_pt); +int icl_ioty_add_generated_pin_cb(iotcon_generated_pin_cb cb, void *user_data); +int icl_ioty_remove_generated_pin_cb(iotcon_generated_pin_cb cb); int icl_ioty_set_device_info(const char *device_name); int icl_ioty_set_platform_info(); @@ -125,4 +125,34 @@ API int iotcon_set_timeout(int timeout_seconds) return IOTCON_ERROR_NONE; } +API int iotcon_add_generated_pin_cb(iotcon_generated_pin_cb cb, void *user_data) +{ + int ret; + + RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + ret = icl_ioty_add_generated_pin_cb(cb, user_data); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_set_generate_pin_cb() Fail(%d)", ret); + return ret; + } + + return IOTCON_ERROR_NONE; +} +API int iotcon_remove_generated_pin_cb(iotcon_generated_pin_cb cb) +{ + int ret; + + RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED); + RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER); + + ret = icl_ioty_remove_generated_pin_cb(cb); + if (IOTCON_ERROR_NONE != ret) { + ERR("icl_ioty_remove_generated_pin_cb() Fail(%d)", ret); + return ret; + } + + return IOTCON_ERROR_NONE; +} |