• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const KeyBlob & authToken,uint32_t flag,const std::string & mnt)24 int32_t FscryptKeyV2::ActiveKey(const KeyBlob &authToken, 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(const KeyBlob & authToken,uint32_t flag,uint32_t sdpClass,const std::string & mnt)137 int32_t FscryptKeyV2::UnlockUserScreen(const KeyBlob &authToken, uint32_t flag,
138     uint32_t sdpClass, const std::string &mnt)
139 {
140     (void)mnt;
141     (void)flag;
142     LOGI("Unsupported fscrypt v2");
143     return E_OK;
144 }
145 
GenerateAppkey(uint32_t userId,uint32_t hashId,std::string & keyId)146 int32_t FscryptKeyV2::GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId)
147 {
148     LOGI("Unsupported fscrypt v2");
149     return E_NOT_SUPPORT;
150 }
151 
DeleteAppkey(const std::string keyId)152 int32_t FscryptKeyV2::DeleteAppkey(const std::string keyId)
153 {
154     LOGI("Unsupported fscrypt v2");
155     return E_NOT_SUPPORT;
156 }
157 
AddClassE(bool & isNeedEncryptClassE,bool & isSupport,uint32_t status)158 int32_t FscryptKeyV2::AddClassE(bool &isNeedEncryptClassE, bool &isSupport, uint32_t status)
159 {
160     (void)isNeedEncryptClassE;
161     (void)status;
162     (void)isSupport;
163     LOGI("Unsupported fscrypt v2");
164     return E_OK;
165 }
166 
DeleteClassEPinCode(uint32_t user)167 int32_t FscryptKeyV2::DeleteClassEPinCode(uint32_t user)
168 {
169     (void)user;
170     LOGI("Unsupported fscrypt v2");
171     return E_OK;
172 }
173 
ChangePinCodeClassE(bool & isFbeSupport,uint32_t userId)174 int32_t FscryptKeyV2::ChangePinCodeClassE(bool &isFbeSupport, uint32_t userId)
175 {
176     (void)userId;
177     isFbeSupport = false;
178     LOGI("Unsupported fscrypt v2");
179     return E_OK;
180 }
181 
UpdateClassEBackUp(uint32_t userId)182 int32_t FscryptKeyV2::UpdateClassEBackUp(uint32_t userId)
183 {
184     (void)userId;
185     LOGI("Unsupported fscrypt v2");
186     return E_OK;
187 }
188 
DecryptClassE(const UserAuth & auth,bool & isSupport,bool & eBufferStatue,uint32_t user,bool needSyncCandidate)189 int32_t FscryptKeyV2::DecryptClassE(const UserAuth &auth, bool &isSupport,
190                                     bool &eBufferStatue, uint32_t user, bool needSyncCandidate)
191 {
192     (void)auth;
193     (void)user;
194     (void)needSyncCandidate;
195     isSupport = false;
196     eBufferStatue = false;
197     LOGI("Unsupported fscrypt v2");
198     return E_OK;
199 }
200 
EncryptClassE(const UserAuth & auth,bool & isSupport,uint32_t user,uint32_t status)201 int32_t FscryptKeyV2::EncryptClassE(const UserAuth &auth, bool &isSupport, uint32_t user, uint32_t status)
202 {
203     (void)auth;
204     (void)user;
205     (void)status;
206     isSupport = false;
207     LOGI("Unsupported fscrypt v2");
208     return E_OK;
209 }
210 } // namespace StorageDaemon
211 } // namespace OHOS
212