1 /*
2 * Copyright (c) 2022-2025 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
16 #include "fscrypt_key_v2.h"
17
18 #include "storage_service_errno.h"
19 #include "storage_service_log.h"
20
21 namespace OHOS {
22 namespace StorageDaemon {
23 #ifdef SUPPORT_FSCRYPT_V2
ActiveKey(uint32_t flag,const std::string & mnt)24 int32_t FscryptKeyV2::ActiveKey(uint32_t flag, const std::string &mnt)
25 {
26 LOGI("enter");
27 if (keyInfo_.key.IsEmpty()) {
28 LOGE("rawkey is null");
29 return E_KEY_EMPTY_ERROR;
30 }
31
32 auto buf = std::make_unique<char[]>(sizeof(fscrypt_add_key_arg) + FSCRYPT_MAX_KEY_SIZE);
33 auto arg = reinterpret_cast<fscrypt_add_key_arg *>(buf.get());
34 (void)memset_s(arg, sizeof(fscrypt_add_key_arg) + FSCRYPT_MAX_KEY_SIZE, 0, sizeof(fscrypt_add_key_arg) +
35 FSCRYPT_MAX_KEY_SIZE);
36 arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
37 arg->raw_size = keyInfo_.key.size;
38 auto err = memcpy_s(arg->raw, FSCRYPT_MAX_KEY_SIZE, keyInfo_.key.data.get(), keyInfo_.key.size);
39 if (err != EOK) {
40 LOGE("memcpy failed ret %{public}d", err);
41 return err;
42 }
43
44 if (!KeyCtrlInstallKey(mnt.c_str(), arg)) {
45 LOGE("InstallKey failed");
46 return E_KEY_CTRL_INSTALL_ERROR;
47 }
48 keyInfo_.keyId.Alloc(FSCRYPT_KEY_IDENTIFIER_SIZE);
49 auto ret = memcpy_s(keyInfo_.keyId.data.get(), keyInfo_.keyId.size, arg->key_spec.u.identifier,
50 FSCRYPT_KEY_IDENTIFIER_SIZE);
51 if (ret != EOK) {
52 LOGE("memcpy_s failed ret %{public}d", ret);
53 return ret;
54 }
55 (void)memset_s(arg, sizeof(fscrypt_add_key_arg) + FSCRYPT_MAX_KEY_SIZE, 0, sizeof(fscrypt_add_key_arg) +
56 FSCRYPT_MAX_KEY_SIZE);
57
58 LOGI("success. key_id len:%{public}d, data(hex):%{private}s", keyInfo_.keyId.size,
59 keyInfo_.keyId.ToString().c_str());
60 if (!SaveKeyBlob(keyInfo_.keyId, dir_ + PATH_KEYID)) {
61 return E_SAVE_KEY_BLOB_ERROR;
62 }
63 keyInfo_.key.Clear();
64 LOGI("success");
65 return E_OK;
66 }
67
InactiveKey(uint32_t flag,const std::string & mnt)68 int32_t FscryptKeyV2::InactiveKey(uint32_t flag, const std::string &mnt)
69 {
70 LOGI("enter");
71 if (keyInfo_.keyId.size == 0) {
72 LOGE("keyId size is 0");
73 return E_OK;
74 }
75 if (keyInfo_.keyId.size != FSCRYPT_KEY_IDENTIFIER_SIZE) {
76 LOGE("keyId is invalid, %{public}u", keyInfo_.keyId.size);
77 return E_INVAILd_KEY_ID_ERROR;
78 }
79
80 fscrypt_remove_key_arg arg;
81 (void)memset_s(&arg, sizeof(arg), 0, sizeof(arg));
82 arg.key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
83 auto ret = memcpy_s(arg.key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE, keyInfo_.keyId.data.get(),
84 keyInfo_.keyId.size);
85 if (ret != EOK) {
86 LOGE("memcpy_s failed ret %{public}d", ret);
87 return ret;
88 }
89
90 if (!KeyCtrlRemoveKey(mnt.c_str(), &arg)) {
91 return E_REMOVE_KEY_ERROR;
92 }
93 if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
94 LOGE("Other users still have this key added");
95 } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
96 LOGE("Some files using this key are still in-use");
97 }
98
99 LOGI("success");
100 keyInfo_.keyId.Clear();
101 return E_OK;
102 }
103
104 #else
105 int32_t FscryptKeyV2::ActiveKey(uint32_t flag, const std::string &mnt)
106 {
107 (void)mnt;
108 (void)flag;
109 LOGI("Unsupported fscrypt v2");
110 return E_PARAMS_INVALID;
111 }
112
113 int32_t FscryptKeyV2::InactiveKey(uint32_t flag, const std::string &mnt)
114 {
115 (void)mnt;
116 (void)flag;
117 LOGI("Unsupported fscrypt v2");
118 return E_PARAMS_INVALID;
119 }
120 #endif
121
LockUserScreen(uint32_t flag,uint32_t sdpClass,const std::string & mnt)122 int32_t FscryptKeyV2::LockUserScreen(uint32_t flag, uint32_t sdpClass, const std::string &mnt)
123 {
124 (void)mnt;
125 (void)flag;
126 LOGI("Unsupported fscrypt v2");
127 return E_OK;
128 }
129
LockUece(bool & isFbeSupport)130 int32_t FscryptKeyV2::LockUece(bool &isFbeSupport)
131 {
132 isFbeSupport = false;
133 LOGI("Unsupported fscrypt v2");
134 return E_OK;
135 }
136
UnlockUserScreen(uint32_t flag,uint32_t sdpClass,const std::string & mnt)137 int32_t FscryptKeyV2::UnlockUserScreen(uint32_t flag, uint32_t sdpClass, const std::string &mnt)
138 {
139 (void)mnt;
140 (void)flag;
141 LOGI("Unsupported fscrypt v2");
142 return E_OK;
143 }
144
GenerateAppkey(uint32_t userId,uint32_t hashId,std::string & keyId)145 int32_t FscryptKeyV2::GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId)
146 {
147 LOGI("Unsupported fscrypt v2");
148 return E_NOT_SUPPORT;
149 }
150
DeleteAppkey(const std::string KeyId)151 int32_t FscryptKeyV2::DeleteAppkey(const std::string KeyId)
152 {
153 LOGI("Unsupported fscrypt v2");
154 return E_NOT_SUPPORT;
155 }
156
AddClassE(bool & isNeedEncryptClassE,bool & isSupport,uint32_t status)157 int32_t FscryptKeyV2::AddClassE(bool &isNeedEncryptClassE, bool &isSupport, uint32_t status)
158 {
159 (void)isNeedEncryptClassE;
160 (void)status;
161 (void)isSupport;
162 LOGI("Unsupported fscrypt v2");
163 return E_OK;
164 }
165
DeleteClassEPinCode(uint32_t user)166 int32_t FscryptKeyV2::DeleteClassEPinCode(uint32_t user)
167 {
168 (void)user;
169 LOGI("Unsupported fscrypt v2");
170 return E_OK;
171 }
172
ChangePinCodeClassE(bool & isFbeSupport,uint32_t userId)173 int32_t FscryptKeyV2::ChangePinCodeClassE(bool &isFbeSupport, uint32_t userId)
174 {
175 (void)userId;
176 isFbeSupport = false;
177 LOGI("Unsupported fscrypt v2");
178 return E_OK;
179 }
180
DecryptClassE(const UserAuth & auth,bool & isSupport,bool & eBufferStatue,uint32_t user,bool needSyncCandidate)181 int32_t FscryptKeyV2::DecryptClassE(const UserAuth &auth, bool &isSupport,
182 bool &eBufferStatue, uint32_t user, bool needSyncCandidate)
183 {
184 (void)auth;
185 (void)user;
186 (void)needSyncCandidate;
187 isSupport = false;
188 eBufferStatue = false;
189 LOGI("Unsupported fscrypt v2");
190 return E_OK;
191 }
192
EncryptClassE(const UserAuth & auth,bool & isSupport,uint32_t user,uint32_t status)193 int32_t FscryptKeyV2::EncryptClassE(const UserAuth &auth, bool &isSupport, uint32_t user, uint32_t status)
194 {
195 (void)auth;
196 (void)user;
197 (void)status;
198 isSupport = false;
199 LOGI("Unsupported fscrypt v2");
200 return E_OK;
201 }
202 } // namespace StorageDaemon
203 } // namespace OHOS
204