Add recovery APIs to use when there is something wrong with encryption
[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-password.h"
21
22 static popup_data_s popup_data = {0,};
23
24 void on_create_password_popup(Evas_Object *parent)
25 {
26         popup_data.parent = parent;
27         popup_data.mode = CREATE_PASSWORD;
28         create_password_message_popup(&popup_data);
29         return;
30 }
31
32 void on_change_password_popup(Evas_Object *parent)
33 {
34         popup_data.parent = parent;
35         popup_data.mode = CHANGE_PASSWORD;
36         create_confirm_password_popup(&popup_data);
37         return;
38 }
39
40 void on_delete_password_popup(Evas_Object *parent)
41 {
42         popup_data.parent = parent;
43         popup_data.mode = DELETE_PASSWORD;
44         create_password_message_popup(&popup_data);
45         return;
46 }
47
48 void on_insert_sdcard_popup(Evas_Object *parent)
49 {
50         popup_data.parent = parent;
51         popup_data.mode = INSERT_SD_CARD;
52         popup_data.callback = NULL;
53         create_insert_sdcard_popup(&popup_data);
54         return;
55 }
56
57 void password_popup_set_result_callback(void (*callback)(void *), void *user_data)
58 {
59         popup_data.callback = callback;
60         popup_data.callback_data = user_data;
61 }
62
63 void password_result_callback(popup_data_s *data, const char *result)
64 {
65         int ret = 0;
66         appdata_s *ad = NULL;
67
68         if (!strcmp(result, "fail")) {
69                 return;
70         }
71
72         if (data->callback_data)
73                 ad = (appdata_s *)data->callback_data;
74
75         switch (data->mode) {
76         case CREATE_PASSWORD:
77                 ret = ode_external_encryption_init_password(data->entry_data);
78                 if (ret != ODE_ERROR_NONE) {
79                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to create external data");
80                         return;
81                 }
82                 ad->entry_data = data->entry_data;
83                 ad->device_info.sdcard_pw_status = 1;
84                 break;
85         case CHANGE_PASSWORD:
86                 ret = ode_external_encryption_change_password(data->entry_data_old, data->entry_data);
87                 if (ret != ODE_ERROR_NONE) {
88                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to change external data");
89                         return;
90                 }
91                 ad->entry_data = data->entry_data;
92                 ad->device_info.sdcard_pw_status = 1;
93                 break;
94         case DELETE_PASSWORD:
95                 ret = ode_external_encryption_clean_password(data->entry_data);
96                 if (ret != ODE_ERROR_NONE) {
97                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to delete external data");
98                         return;
99                 }
100                 ad->entry_data = NULL;
101                 ad->device_info.sdcard_pw_status = 0;
102                 break;
103         case INSERT_SD_CARD:
104                 ret = ode_external_encryption_mount(data->entry_data);
105                 if (ret != ODE_ERROR_NONE) {
106                         dlog_print(DLOG_ERROR, LOG_TAG, "failed to mount external storage");
107                         return;
108                 }
109                 break;
110         default:
111                 break;
112         }
113
114         if (data->callback)
115                 data->callback(data->callback_data);
116         return;
117 }