• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "libfscrypt/key_control.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 bool FscryptKeyV2::ActiveKey(uint32_t flag, const std::string &mnt)
25 {
26     LOGD("enter");
27     if (keyInfo_.key.IsEmpty()) {
28         LOGE("rawkey is null");
29         return false;
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 false;
42     }
43 
44     if (!KeyCtrlInstallKey(mnt.c_str(), arg)) {
45         LOGE("InstallKey failed");
46         return false;
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 false;
54     }
55 
56     LOGD("success. key_id len:%{public}d, data(hex):%{private}s", keyInfo_.keyId.size,
57         keyInfo_.keyId.ToString().c_str());
58     if (!SaveKeyBlob(keyInfo_.keyId, dir_ + PATH_KEYID)) {
59         return false;
60     }
61     keyInfo_.key.Clear();
62     LOGD("success");
63     return true;
64 }
65 
InactiveKey(uint32_t flag,const std::string & mnt)66 bool FscryptKeyV2::InactiveKey(uint32_t flag, const std::string &mnt)
67 {
68     LOGD("enter");
69     if (keyInfo_.keyId.size != FSCRYPT_KEY_IDENTIFIER_SIZE) {
70         LOGE("keyId is invalid, %{public}u", keyInfo_.keyId.size);
71         return false;
72     }
73 
74     fscrypt_remove_key_arg arg;
75     (void)memset_s(&arg, sizeof(arg), 0, sizeof(arg));
76     arg.key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
77     auto ret = memcpy_s(arg.key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE, keyInfo_.keyId.data.get(),
78         keyInfo_.keyId.size);
79     if (ret != EOK) {
80         LOGE("memcpy_s failed ret %{public}d", ret);
81         return false;
82     }
83 
84     if (!KeyCtrlRemoveKey(mnt.c_str(), &arg)) {
85         return false;
86     }
87     if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
88         LOGE("Other users still have this key added");
89     } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
90         LOGE("Some files using this key are still in-use");
91     }
92 
93     LOGD("success");
94     keyInfo_.keyId.Clear();
95     return true;
96 }
97 
98 #else
99 bool FscryptKeyV2::ActiveKey(uint32_t flag, const std::string &mnt)
100 {
101     (void)mnt;
102     (void)flag;
103     LOGI("Unsupported fscrypt v2");
104     return false;
105 }
106 
107 bool FscryptKeyV2::InactiveKey(uint32_t flag, const std::string &mnt)
108 {
109     (void)mnt;
110     (void)flag;
111     LOGI("Unsupported fscrypt v2");
112     return false;
113 }
114 #endif
115 } // namespace StorageDaemon
116 } // namespace OHOS
117