Set the length of PMK
authorCheoleun Moon <chleun.moon@samsung.com>
Wed, 11 Mar 2020 10:06:32 +0000 (19:06 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Wed, 11 Mar 2020 10:06:32 +0000 (19:06 +0900)
include/wifi-aware.h
src/include/wifi-aware-data-path.h
src/include/wifi-aware-gdbus.h
src/include/wifi-aware-private.h
src/wifi-aware-data-path.c
src/wifi-aware-gdbus.c
src/wifi-aware.c
test/wifi-aware-test.c

index 08fcb27..818b87e 100644 (file)
@@ -634,9 +634,10 @@ int wifi_aware_data_path_set_psk(wifi_aware_data_path_h data_path, const char *k
  * @brief Set a PMK for Wi-Fi Aware Data Path. It can be used only when secury_type is WIFI_AWARE_SECURITY_TYPE_PMK.
  * @since_tizen 6.0
  * @param[in] data_path
- * @param[in] key Pairwise Master Key. Its length should be 32
+ * @param[in] key Pairwise Master Key
+ * @param[in] pmk_len
  */
-int wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const unsigned char *key);
+int wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const unsigned char *key, size_t pmk_len);
 
 // only for responder
 /**
index 774b73a..7449a02 100644 (file)
@@ -29,7 +29,7 @@ extern "C" {
 
 #define WIFI_AWARE_MIN_PSK_LEN 8
 #define WIFI_AWARE_MAX_PSK_LEN 63
-#define WIFI_AWARE_PMK_LEN 32
+#define WIFI_AWARE_MAX_PMK_LEN 32
 #define WIFI_AWARE_STR_IPV6_ADDRESS_LEN 39
 
 typedef enum {
@@ -44,7 +44,7 @@ int _wifi_aware_data_path_set_security(wifi_aware_data_path_h data_path,
                wifi_aware_security_type_e security_type);
 int _wifi_aware_data_path_set_psk(wifi_aware_data_path_h data_path, const char *key);
 int _wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path,
-               const unsigned char *key);
+               const unsigned char *key, size_t pmk_len);
 int _wifi_aware_data_path_set_service_specific_info(wifi_aware_data_path_h data_path,
                const unsigned char *service_specific_info, size_t service_specific_info_len);
 int _wifi_aware_data_path_set_port(wifi_aware_data_path_h data_path, int port);
index d2deb8c..e5b81d6 100644 (file)
@@ -90,7 +90,7 @@ int wifi_aware_gdbus_open_ndp_pmk(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
                const char *service_name,
                wifi_aware_data_path_role_e role, int port,
-               const unsigned char *pmk);
+               const unsigned char *pmk, size_t pmk_len);
 int wifi_aware_gdbus_ndp_close(wifi_aware_data_path_h data_path,
                int client_id, unsigned int ndp_id, uint16_t pub_sub_id, unsigned int peer_id);
 #ifdef __cplusplus
index 00346f6..bdbfd30 100644 (file)
@@ -58,7 +58,7 @@ void _wifi_aware_publish_set_service_specific_info(wifi_aware_publish_h publish,
 void _wifi_aware_publish_set_match_filter(wifi_aware_publish_h publish,
                const unsigned char *match_filter, size_t len);
 void _wifi_aware_publish_set_enable_termination_event(wifi_aware_publish_h publish, bool enable);
-void _wifi_aware_publish_set_enable_ranging(wifi_aware_publish_h publish,      bool enable);
+void _wifi_aware_publish_set_enable_ranging(wifi_aware_publish_h publish, bool enable);
 wifi_aware_publish_request_s *_wifi_aware_publish_get_request(wifi_aware_publish_h publish_handle);
 uint16_t _wifi_aware_session_get_publish_id(wifi_aware_session_h session);
 
index c0d7473..509e517 100644 (file)
@@ -43,7 +43,7 @@ typedef struct {
        wifi_aware_data_path_role_e role;
        wifi_aware_security_type_e security_type;
        char psk[WIFI_AWARE_MAX_PSK_LEN + 1];
-       unsigned char pmk[WIFI_AWARE_PMK_LEN];
+       unsigned char pmk[WIFI_AWARE_MAX_PMK_LEN];
        size_t pmk_len;
        int port;
 
@@ -153,15 +153,20 @@ int _wifi_aware_data_path_set_security(wifi_aware_data_path_h data_path,
        return WIFI_AWARE_ERROR_NONE;
 }
 
-static bool __is_valid_psk_len(const char *key)
+static bool __is_valid_psk_len(const char *psk)
 {
-       size_t len = strlen(key);
+       size_t len = strlen(psk);
        WIFI_AWARE_LOGD("PSK len: %d", len);
 
        return len >= WIFI_AWARE_MIN_PSK_LEN && len <= WIFI_AWARE_MAX_PSK_LEN;
 }
 
-int _wifi_aware_data_path_set_psk(wifi_aware_data_path_h data_path, const char *key)
+static bool __is_valid_pmk_len(size_t pmk_len)
+{
+       return pmk_len <= WIFI_AWARE_MAX_PMK_LEN;
+}
+
+int _wifi_aware_data_path_set_psk(wifi_aware_data_path_h data_path, const char *psk)
 {
        __WIFI_AWARE_FUNC_ENTER__;
 
@@ -171,16 +176,16 @@ int _wifi_aware_data_path_set_psk(wifi_aware_data_path_h data_path, const char *
        RET_VAL_IF(ndp->security_type != WIFI_AWARE_SECURITY_TYPE_PSK,
                WIFI_AWARE_ERROR_INVALID_OPERATION, "security type is not WIFI_AWARE_SECURITY_TYPE_PSK");
 
-       if (!__is_valid_psk_len(key)) {
+       if (!__is_valid_psk_len(psk)) {
                WIFI_AWARE_LOGE("The length of the password should be equal to or greater than 8 and less than 64");
                return WIFI_AWARE_ERROR_INVALID_PARAMETER;
        }
 
-       strncpy(ndp->psk, key, WIFI_AWARE_MAX_PSK_LEN);
+       strncpy(ndp->psk, psk, WIFI_AWARE_MAX_PSK_LEN);
        return WIFI_AWARE_ERROR_NONE;
 }
 
-int _wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const unsigned char *key)
+int _wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const unsigned char *pmk, size_t pmk_len)
 {
        __WIFI_AWARE_FUNC_ENTER__;
 
@@ -189,7 +194,12 @@ int _wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const unsign
        RET_VAL_IF(ndp->security_type != WIFI_AWARE_SECURITY_TYPE_PMK,
                WIFI_AWARE_ERROR_INVALID_OPERATION, "security type is not WIFI_AWARE_SECURITY_TYPE_PMK");
 
-       memcpy(ndp->pmk, key, WIFI_AWARE_PMK_LEN);
+       if (!__is_valid_pmk_len(pmk_len)) {
+               WIFI_AWARE_LOGE("Invalid PMK length");
+               return WIFI_AWARE_ERROR_INVALID_PARAMETER;
+       }
+
+       memcpy(ndp->pmk, pmk, pmk_len);
        return WIFI_AWARE_ERROR_NONE;
 }
 
@@ -286,6 +296,18 @@ static void __data_path_set_data_path_confirmed_cb(wifi_aware_data_path_h data_p
                                __ndp_confirmed_cb, data_path);
 }
 
+static bool __check_psk(wifi_aware_data_path_h data_path)
+{
+       wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
+       return strlen(ndp->psk) > 0;
+}
+
+static bool __check_pmk(wifi_aware_data_path_h data_path)
+{
+       wifi_aware_data_path_s *ndp = (wifi_aware_data_path_s *)data_path;
+       return ndp->pmk_len > 0;
+}
+
 int _wifi_aware_data_path_open(wifi_aware_data_path_h data_path,
                wifi_aware_data_path_open_cb callback, void *user_data)
 {
@@ -308,6 +330,7 @@ int _wifi_aware_data_path_open(wifi_aware_data_path_h data_path,
                                ndp->port);
                break;
        case WIFI_AWARE_SECURITY_TYPE_PSK:
+               RET_VAL_IF(!__check_psk(data_path), WIFI_AWARE_ERROR_INVALID_OPERATION, "Invalid PSK");
                ret = wifi_aware_gdbus_open_ndp_psk(data_path,
                                _wifi_aware_get_client_id(),
                                _wifi_aware_session_get_pub_sub_id(ndp->session),
@@ -318,6 +341,7 @@ int _wifi_aware_data_path_open(wifi_aware_data_path_h data_path,
                                ndp->psk);
                break;
        case WIFI_AWARE_SECURITY_TYPE_PMK:
+               RET_VAL_IF(!__check_pmk(data_path), WIFI_AWARE_ERROR_INVALID_OPERATION, "Invalid PMK");
                ret = wifi_aware_gdbus_open_ndp_pmk(data_path,
                                _wifi_aware_get_client_id(),
                                _wifi_aware_session_get_pub_sub_id(ndp->session),
@@ -325,7 +349,8 @@ int _wifi_aware_data_path_open(wifi_aware_data_path_h data_path,
                                _wifi_aware_session_get_service_name(ndp->session),
                                ndp->role,
                                ndp->port,
-                               ndp->pmk);
+                               ndp->pmk,
+                               ndp->pmk_len);
                break;
        default:
                ret = WIFI_AWARE_ERROR_INVALID_PARAMETER;
index 0c04709..63c41f5 100644 (file)
@@ -382,9 +382,9 @@ static GVariant *__append_param_psk(GVariant *params, const char *psk)
        return g_variant_new("vs", params, psk);
 }
 
-static GVariant *__append_param_pmk(GVariant *params, const unsigned char *pmk)
+static GVariant *__append_param_pmk(GVariant *params, const unsigned char *pmk, size_t pmk_len)
 {
-       return g_variant_new("vv", params, __pack_byte_array(pmk, WIFI_AWARE_PMK_LEN));
+       return g_variant_new("uvv", pmk_len, params, __pack_byte_array(pmk, pmk_len));
 }
 
 static GVariant *__create_ndp_close_request_param(int client_id, unsigned int ndp_id,
@@ -734,7 +734,8 @@ int wifi_aware_gdbus_open_ndp_psk(wifi_aware_data_path_h data_path,
 int wifi_aware_gdbus_open_ndp_pmk(wifi_aware_data_path_h data_path,
                int client_id, uint16_t pub_sub_id, unsigned int peer_id,
                const char *service_name,
-               wifi_aware_data_path_role_e role, int port, const unsigned char *pmk)
+               wifi_aware_data_path_role_e role, int port,
+               const unsigned char *pmk, size_t pmk_len)
 {
        __WIFI_AWARE_FUNC_ENTER__;
        GVariant *params = NULL;
@@ -744,7 +745,7 @@ int wifi_aware_gdbus_open_ndp_pmk(wifi_aware_data_path_h data_path,
 
        params = __create_ndp_open_request_param(client_id, pub_sub_id, peer_id,
                        service_name, role, port);
-       params_pmk = __append_param_pmk(params, pmk);
+       params_pmk = __append_param_pmk(params, pmk, pmk_len);
        g_variant_unref(params);
        WIFI_AWARE_LOGD("created parameters");
        DBUS_DEBUG_VARIANT(params_pmk);
index 66f4e4b..83bf2d2 100644 (file)
@@ -624,7 +624,7 @@ API int wifi_aware_data_path_set_psk(wifi_aware_data_path_h data_path, const cha
        return ret;
 }
 
-API int wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const unsigned char *key)
+API int wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const unsigned char *key, size_t pmk_len)
 {
        int ret = WIFI_AWARE_ERROR_NONE;
 
@@ -633,7 +633,7 @@ API int wifi_aware_data_path_set_pmk(wifi_aware_data_path_h data_path, const uns
        RET_VAL_IF(!_wifi_aware_is_initialized(), WIFI_AWARE_ERROR_NOT_INITIALIZED, "Not initialized");
        RET_VAL_IF(data_path == NULL, WIFI_AWARE_ERROR_INVALID_PARAMETER, "Data path is NULL");
 
-       ret = _wifi_aware_data_path_set_pmk(data_path, key);
+       ret = _wifi_aware_data_path_set_pmk(data_path, key, pmk_len);
 
        __WIFI_AWARE_FUNC_EXIT__;
        return ret;
index 026945f..5a81beb 100644 (file)
@@ -898,13 +898,15 @@ void test_open_data_path()
 
        if (security_type == 2) {
                char key[PMK_LEN] = {0, };
+               size_t pmk_len;
                unsigned char pmk[PMK_LEN] = {0, };
                printf("PMK: ");
                ret = scanf("%s", key);
                if (ret < 0)
                        return;
+               pmk_len = strlen(key);
                memcpy(pmk, key, PMK_LEN);
-               ret = wifi_aware_data_path_set_pmk(g_ndp, pmk);
+               ret = wifi_aware_data_path_set_pmk(g_ndp, pmk, pmk_len);
                __print_result(ret, "wifi_aware_data_path_set_pmk");
        }