Add security policy
[platform/core/security/dpm-security.git] / api / security.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #include <tizen.h>
18 #include <tizen_type.h>
19
20 #include <dpm/pil/policy-client.h>
21
22 #include "security.h"
23
24 EXPORT_API int dpm_security_lockout_screen(device_policy_manager_h handle)
25 {
26         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
27
28         DevicePolicyClient &client = GetDevicePolicyClient(handle);
29         try {
30                 Status<int> status { -1 };
31                 status = client.methodCall<int>("Security::lockoutScreen");
32                 return status.get();
33         } catch (...) {
34                 return -1;
35         }
36 }
37
38 EXPORT_API int dpm_security_set_internal_storage_encryption(device_policy_manager_h handle, bool encrypt)
39 {
40         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
41
42         DevicePolicyClient &client = GetDevicePolicyClient(handle);
43         try {
44                 Status<int> status { -1 };
45                 status = client.methodCall<int>("Security::setInternalStorageEncryption", encrypt);
46                 return status.get();
47         } catch (...) {
48                 return -1;
49         }
50 }
51
52 EXPORT_API int dpm_security_is_internal_storage_encrypted(device_policy_manager_h handle, bool *is_encrypted)
53 {
54         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
55         RET_ON_FAILURE(is_encrypted, DPM_ERROR_INVALID_PARAMETER);
56
57         DevicePolicyClient &client = GetDevicePolicyClient(handle);
58         try {
59                 Status<bool> status { false };
60                 status = client.methodCall<int>("Security::isInternalStorageEncrypted");
61                 *is_encrypted = status.get();
62         } catch (...) {
63                 return -1;
64         }
65
66         return DPM_ERROR_NONE;
67 }
68
69 EXPORT_API int dpm_security_set_external_storage_encryption(device_policy_manager_h handle, bool encrypt)
70 {
71         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
72
73         DevicePolicyClient &client = GetDevicePolicyClient(handle);
74         try {
75                 Status<int> status { -1 };
76                 status = client.methodCall<int>("Security::setExternalStorageEncryption", encrypt);
77                 return status.get();
78         } catch (...) {
79                 return -1;
80         }
81 }
82
83 EXPORT_API int dpm_security_is_external_storage_encrypted(device_policy_manager_h handle, bool *is_encrypted)
84 {
85         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
86         RET_ON_FAILURE(is_encrypted, DPM_ERROR_INVALID_PARAMETER);
87
88         DevicePolicyClient &client = GetDevicePolicyClient(handle);
89         try {
90                 Status<bool> status { false };
91                 status = client.methodCall<int>("Security::isExternalStorageEncrypted");
92                 *is_encrypted = status.get();
93         } catch (...) {
94                 return -1;
95         }
96
97         return DPM_ERROR_NONE;
98 }
99
100 EXPORT_API int dpm_security_wipe_data(device_policy_manager_h handle, dpm_security_wipe_type_e type)
101 {
102         int mask = type & (DPM_SECURITY_WIPE_INTERNAL_STORAGE | DPM_SECURITY_WIPE_EXTERNAL_STORAGE);
103
104         RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER);
105         RET_ON_FAILURE(mask, DPM_ERROR_INVALID_PARAMETER);
106
107         DevicePolicyClient &client = GetDevicePolicyClient(handle);
108         try {
109                 Status<int> status { -1 };
110                 status = client.methodCall<int>("Security::wipeData", mask);
111                 return status.get();
112         } catch (...) {
113                 return -1;
114         }
115 }