Add recovery APIs to use when there is something wrong with encryption
[platform/core/security/ode.git] / lib / ode / internal-encryption.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 "debug.h"
18 #include "internal-encryption.h"
19
20 #include "client.h"
21 #include "rmi/internal-encryption.h"
22
23 using namespace ode;
24
25 int ode_internal_encryption_mount(const char* password)
26 {
27         RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
28
29         ODEContext client;
30         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
31         InternalEncryption internal = client.createInterface<InternalEncryption>();
32
33         return internal.mount(password);
34 }
35
36 int ode_internal_encryption_umount()
37 {
38         ODEContext client;
39         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
40         InternalEncryption internal = client.createInterface<InternalEncryption>();
41
42         return internal.umount();
43 }
44
45 int ode_internal_encryption_encrypt(const char* password, unsigned int options)
46 {
47         RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
48
49         ODEContext client;
50         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
51         InternalEncryption internal = client.createInterface<InternalEncryption>();
52
53         return internal.encrypt(password, options);
54 }
55
56 int ode_internal_encryption_decrypt(const char* password)
57 {
58         RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
59
60         ODEContext client;
61         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
62         InternalEncryption internal = client.createInterface<InternalEncryption>();
63
64         return internal.decrypt(password);
65 }
66
67 int ode_internal_encryption_recovery()
68 {
69         ODEContext client;
70         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
71         InternalEncryption internal = client.createInterface<InternalEncryption>();
72
73         return internal.recovery();
74 }
75
76 int ode_internal_encryption_is_password_initialized(bool* result)
77 {
78     RET_ON_FAILURE(result, ODE_ERROR_INVALID_PARAMETER);
79
80     ODEContext client;
81     RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
82         InternalEncryption internal = client.createInterface<InternalEncryption>();
83         int ret = internal.isPasswordInitialized();
84
85         RET_ON_FAILURE(ret >= 0, ODE_ERROR_INVALID_PARAMETER);
86
87         *result = ret;
88     return ODE_ERROR_NONE;
89 }
90
91 int ode_internal_encryption_init_password(const char* password)
92 {
93     RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
94
95     ODEContext client;
96     RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
97         InternalEncryption internal = client.createInterface<InternalEncryption>();
98
99         return internal.initPassword(password);
100 }
101
102 int ode_internal_encryption_clean_password(const char* password)
103 {
104     RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
105
106     ODEContext client;
107     RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
108         InternalEncryption internal = client.createInterface<InternalEncryption>();
109
110         return internal.cleanPassword(password);
111 }
112
113 int ode_internal_encryption_change_password(const char* old_password,
114                                                                                         const char* new_password)
115 {
116         RET_ON_FAILURE(old_password, ODE_ERROR_INVALID_PARAMETER);
117         RET_ON_FAILURE(new_password, ODE_ERROR_INVALID_PARAMETER);
118
119         ODEContext client;
120         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
121         InternalEncryption internal = client.createInterface<InternalEncryption>();
122
123         return internal.changePassword(old_password, new_password);
124 }
125
126 int ode_internal_encryption_verify_password(const char* password, bool* result)
127 {
128     RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
129     RET_ON_FAILURE(result, ODE_ERROR_INVALID_PARAMETER);
130
131     ODEContext client;
132     RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
133         InternalEncryption internal = client.createInterface<InternalEncryption>();
134         int ret = internal.verifyPassword(password);
135
136         RET_ON_FAILURE(ret >= 0, ODE_ERROR_INVALID_PARAMETER);
137
138         *result = ret;
139     return ODE_ERROR_NONE;
140 }
141
142 int ode_internal_encryption_get_state(int* state)
143 {
144         RET_ON_FAILURE(state, ODE_ERROR_INVALID_PARAMETER);
145
146         ODEContext client;
147         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
148         InternalEncryption internal = client.createInterface<InternalEncryption>();
149         int ret = internal.getState();
150
151         RET_ON_FAILURE(ret >= 0, ODE_ERROR_INVALID_PARAMETER);
152
153         *state = ret;
154         return ODE_ERROR_NONE;
155 }
156
157 int ode_internal_encryption_get_supported_options(unsigned int* options)
158 {
159         RET_ON_FAILURE(options, ODE_ERROR_INVALID_PARAMETER);
160
161         ODEContext client;
162         RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
163         InternalEncryption internal = client.createInterface<InternalEncryption>();
164         *options = internal.getSupportedOptions();
165         return ODE_ERROR_NONE;
166 }