38f19dbe1fefc4d5677b92d9760fe33f7a056ecd
[platform/core/security/ode.git] / tools / apps / ode / src / password / password.c
1 /*
2  *
3  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <ode/external-encryption.h>
20 #include <ode/internal-encryption.h>
21 #include "ode-password.h"
22
23 static popup_data_s popup_data = {0,};
24
25 void on_create_password_popup(Evas_Object *parent)
26 {
27         popup_data.parent = parent;
28         popup_data.mode = CREATE_PASSWORD;
29         create_password_message_popup(&popup_data);
30         return;
31 }
32
33 void on_change_password_popup(Evas_Object *parent)
34 {
35         popup_data.parent = parent;
36         popup_data.mode = CHANGE_PASSWORD;
37         create_confirm_password_popup(&popup_data);
38         return;
39 }
40
41 void on_delete_password_popup(Evas_Object *parent)
42 {
43         popup_data.parent = parent;
44         popup_data.mode = DELETE_PASSWORD;
45         create_password_message_popup(&popup_data);
46         return;
47 }
48
49 void on_insert_sdcard_popup(Evas_Object *parent)
50 {
51         popup_data.parent = parent;
52         popup_data.mode = INSERT_SD_CARD;
53         popup_data.callback = NULL;
54         create_insert_sdcard_popup(&popup_data);
55         return;
56 }
57
58 void password_popup_set_result_callback(void (*callback)(void *), void *user_data)
59 {
60         popup_data.callback = callback;
61         popup_data.callback_data = user_data;
62 }
63
64 void password_result_callback(popup_data_s *data, const char *result)
65 {
66         int ret = 0;
67         appdata_s *ad = NULL;
68
69         if (!strcmp(result, "fail")) {
70                 return;
71         }
72
73         if (data->callback_data)
74                 ad = (appdata_s *)data->callback_data;
75
76         switch (data->mode) {
77         case CREATE_PASSWORD:
78                 ret = ode_external_encryption_init_password(data->entry_data);
79                 if (ret != ODE_ERROR_NONE) {
80                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to create external data");
81                         return;
82                 }
83                 ad->entry_data = data->entry_data;
84                 ad->device_info.sdcard_pw_status = 1;
85                 break;
86         case CHANGE_PASSWORD:
87                 ret = ode_external_encryption_change_password(data->entry_data_old, data->entry_data);
88                 if (ret != ODE_ERROR_NONE) {
89                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to change external data");
90                         return;
91                 }
92                 ad->entry_data = data->entry_data;
93                 ad->device_info.sdcard_pw_status = 1;
94                 break;
95         case DELETE_PASSWORD:
96                 ret = ode_external_encryption_clean_password(data->entry_data);
97                 if (ret != ODE_ERROR_NONE) {
98                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to delete external data");
99                         return;
100                 }
101                 ad->entry_data = NULL;
102                 ad->device_info.sdcard_pw_status = 0;
103                 break;
104         case INSERT_SD_CARD:
105                 ret = ode_external_encryption_mount(data->entry_data);
106                 if (ret != ODE_ERROR_NONE) {
107                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to mount external storage");
108                         return;
109                 }
110                 break;
111         /* for temporary start */
112         case INTERNAL_ENCRYPT_PASSWORD:
113                 ret = ode_internal_encryption_init_password(data->entry_data);
114                 if (ret != ODE_ERROR_NONE) {
115                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to create internal data");
116                         return;
117                 }
118                 ret = ode_internal_encryption_encrypt(data->entry_data, ad->device_info.device_option);
119                 if (ret != 0) {
120                         dlog_print(DLOG_DEBUG, LOG_TAG, "failed to encrypt internal storage");
121                         return;
122                 }
123                 break;
124         case INTERNAL_DECRYPT_PASSWORD:
125                 ret = ode_internal_encryption_decrypt(data->entry_data);
126                 if (ret != 0) {
127                         dlog_print(DLOG_DEBUG, LOG_TAG, "failed to decrypt internal storage");
128                         return;
129                 }
130                 break;
131         /* for temporary end */
132         default:
133                 break;
134         }
135
136         if (data->callback)
137                 data->callback(data->callback_data);
138         return;
139 }