• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "ss_crypto_api.h"
16 
17 #include <iservice_registry.h>
18 #include <system_ability_definition.h>
19 
20 #include "fbe_crypto.h"
21 #include "istorage_service.h"
22 #include "storage_hilog.h"
23 #include "storage_service_proxy.h"
24 
25 namespace OHOS {
26 const int SCA_ERR_NONE = 0;
27 
28 sptr<IStorageService> g_cryptoProxy = nullptr;
29 
GetCryptoProxy()30 bool GetCryptoProxy()
31 {
32     if (g_cryptoProxy != nullptr) {
33         return true;
34     }
35     SSLOG_E("SS_request::Connect start");
36     if (g_cryptoProxy == nullptr) {
37         auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38         if (sam == nullptr) {
39             SSLOG_E("SS_request::Connect samgr == nullptr");
40             return false;
41         }
42         auto object = sam->GetSystemAbility(STORAGE_SERVICE_ID);
43         if (object == nullptr) {
44             SSLOG_E("SS_request::Connect object == nullptr");
45             return false;
46         }
47         g_cryptoProxy = iface_cast<IStorageService>(object);
48         if (g_cryptoProxy == nullptr) {
49             SSLOG_E("SS_request::Connect service == nullptr");
50             return false;
51         }
52     }
53     SSLOG_E("DeviceStorageManager::Connect end");
54     return true;
55 }
56 
SsCryptoEnable()57 int32_t SsCryptoEnable()
58 {
59     SSLOG_I("CryptoEnable starting....(from gby)");
60     if (g_cryptoProxy == nullptr) {
61         SSLOG_E("The mSsProxy is null..(from gby)");
62         return SCA_ERR_NONE;
63     }
64     g_cryptoProxy->CryptoEnable();
65     SSLOG_I("CryptoEnable end....(from gby)");
66     return 0;
67 }
68 
SsCryptoInitialize()69 int32_t SsCryptoInitialize()
70 {
71     SSLOG_I("CryptoInitialize starting....(from gby)");
72     if (g_cryptoProxy == nullptr) {
73         SSLOG_E("The mSsProxy is null..");
74         return SCA_ERR_NONE;
75     }
76     g_cryptoProxy->CryptoInitialize();
77     SSLOG_I("CryptoInitialize end....(from gby)");
78     return 0;
79 }
80 
SsCryptoCreateKey(int32_t userId)81 int32_t SsCryptoCreateKey(int32_t userId)
82 {
83     SSLOG_I("CryptoCreateKey starting....(from gby)");
84     if (g_cryptoProxy == nullptr) {
85         SSLOG_E("The mSsProxy is null..");
86         return SCA_ERR_NONE;
87     }
88     if (userId <= 0) {
89         SSLOG_E("The input userId is illegal");
90         return SCA_ERR_NONE;
91     }
92     g_cryptoProxy->CryptoCreateKey(userId);
93     SSLOG_I("CryptoCreateKey end....(from gby)");
94     return 0;
95 }
96 
SsCryptoDeleteKey(int32_t userId)97 int32_t SsCryptoDeleteKey(int32_t userId)
98 {
99     SSLOG_I("CryptoDeleteKey starting....(from gby)");
100     if (g_cryptoProxy == nullptr) {
101         SSLOG_E("The mSsProxy is null..");
102         return SCA_ERR_NONE;
103     }
104     if (userId <= 0) {
105         SSLOG_E("The input userId is illegal");
106         return SCA_ERR_NONE;
107     }
108     g_cryptoProxy->CryptoDeleteKey(userId);
109     SSLOG_I("CryptoDeleteKey end....(from gby)");
110     return 0;
111 }
112 
SsCryptoAddAuthKey(int32_t userId,const std::string & token,const std::string & secret)113 int32_t SsCryptoAddAuthKey(int32_t userId, const std::string &token, const std::string &secret)
114 {
115     SSLOG_I("CryptoAddAuthKey starting....(from gby)");
116     if (g_cryptoProxy == nullptr) {
117         SSLOG_E("The mSsProxy is null..");
118         return SCA_ERR_NONE;
119     }
120     if (userId <= 0) {
121         SSLOG_E("The input userId is illegal");
122         return SCA_ERR_NONE;
123     }
124     g_cryptoProxy->CryptoAddAuthKey(userId, token, secret);
125     SSLOG_I("CryptoAddAuthKey end....(from gby)");
126     return 0;
127 }
128 
SsCryptoDelAuthKey(int32_t userId,const std::string & token,const std::string & secret)129 int32_t SsCryptoDelAuthKey(int32_t userId, const std::string &token, const std::string &secret)
130 {
131     SSLOG_I("CryptoDelAuthKey starting....(from gby)");
132     if (g_cryptoProxy == nullptr) {
133         SSLOG_E("The mSsProxy is null..");
134         return SCA_ERR_NONE;
135     }
136     if (userId <= 0) {
137         SSLOG_E("The input userId is illegal");
138         return SCA_ERR_NONE;
139     }
140     g_cryptoProxy->CryptoDelAuthKey(userId, token, secret);
141     SSLOG_I("CryptoDelAuthKey end....(from gby)");
142     return 0;
143 }
144 
SsCryptoUnlockKey(int32_t userId,const std::string & token,const std::string & secret)145 int32_t SsCryptoUnlockKey(int32_t userId, const std::string &token, const std::string &secret)
146 {
147     SSLOG_I("CryptoUnlockKey starting....(from gby)");
148     if (g_cryptoProxy == nullptr) {
149         SSLOG_E("The mSsProxy is null..");
150         return SCA_ERR_NONE;
151     }
152     if (userId <= 0) {
153         SSLOG_E("The input userId is illegal");
154         return SCA_ERR_NONE;
155     }
156     g_cryptoProxy->CryptoUnlockKey(userId, token, secret);
157     SSLOG_I("CryptoUnlockKey end....(from gby)");
158     return 0;
159 }
160 
SsCryptoLockKey(int32_t userId)161 int32_t SsCryptoLockKey(int32_t userId)
162 {
163     SSLOG_I("CryptoLockKey starting....(from gby)");
164     if (g_cryptoProxy == nullptr) {
165         SSLOG_E("The mSsProxy is null..");
166         return SCA_ERR_NONE;
167     }
168     if (userId <= 0) {
169         SSLOG_E("The input userId is illegal");
170         return SCA_ERR_NONE;
171     }
172     g_cryptoProxy->CryptoLockKey(userId);
173     SSLOG_I("CryptoLockKey end....(from gby)");
174     return 0;
175 }
176 
SsCryptoUpdateAuthKey(int32_t userId,const std::string & token,const std::string secret)177 int32_t SsCryptoUpdateAuthKey(int32_t userId, const std::string &token, const std::string secret)
178 {
179     SSLOG_I("CryptoUpdateAuthKey starting....(from gby)");
180     if (g_cryptoProxy == nullptr) {
181         SSLOG_E("The mSsProxy is null..");
182         return SCA_ERR_NONE;
183     }
184     if (userId <= 0) {
185         SSLOG_E("The input userId is illegal");
186         return SCA_ERR_NONE;
187     }
188     g_cryptoProxy->CryptoUpdateAuthKey(userId, token, secret);
189     SSLOG_I("CryptoUpdateAuthKey end....(from gby)");
190     return 0;
191 }
192 
SsCryptoInitUserSpace(const std::string & uuid,int32_t userid,int32_t flag)193 int32_t SsCryptoInitUserSpace(const std::string &uuid, int32_t userid, int32_t flag)
194 {
195     SSLOG_I("CryptoCreatePaths starting....(from gby)");
196     if (g_cryptoProxy == nullptr) {
197         SSLOG_E("The mSsProxy is null..");
198         return SCA_ERR_NONE;
199     }
200     if (userid <= 0) {
201         SSLOG_E("The input userId is illegal");
202         return SCA_ERR_NONE;
203     }
204     g_cryptoProxy->CryptoInitUserSpace(uuid, userid, flag);
205     SSLOG_I("CryptoCreatePaths end....(from gby)");
206     return 0;
207 }
208 
SsCryptoRemoveUserSpace(const std::string & uuid,int32_t userid,int32_t flag)209 int32_t SsCryptoRemoveUserSpace(const std::string &uuid, int32_t userid, int32_t flag)
210 {
211     SSLOG_I("CryptoRemovePaths starting....(from gby)");
212     if (g_cryptoProxy == nullptr) {
213         SSLOG_E("The mSsProxy is null..");
214         return SCA_ERR_NONE;
215     }
216     if (userid <= 0) {
217         SSLOG_E("The input userId is illegal");
218         return SCA_ERR_NONE;
219     }
220     g_cryptoProxy->CryptoRemoveUserSpace(uuid, userid, flag);
221     SSLOG_I("CryptoRemovePaths end....(from gby)");
222     return 0;
223 }
224 } // namespace OHOS