• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "user_cert_plugin.h"
17 
18 #include "cert_manager_api.h"
19 #include "cm_type.h"
20 #include "edm_ipc_interface_code.h"
21 #include "edm_log.h"
22 #include "func_code_utils.h"
23 #include "plugin_manager.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 static constexpr uint32_t MAX_URI_LEN = 256;
28 static constexpr uint32_t MAX_ALIAS_LEN = 40;
29 static constexpr uint32_t MAX_CERT_URI_LEN = 64;
30 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(std::make_shared<UserCertPlugin>());
31 
UserCertPlugin()32 UserCertPlugin::UserCertPlugin()
33 {
34     policyCode_ = EdmInterfaceCode::INSTALL_CERTIFICATE;
35     policyName_ = "install_certificate";
36     permissionConfig_.permission = "ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE";
37     permissionConfig_.permissionType = IPlugin::PermissionType::SUPER_DEVICE_ADMIN;
38     permissionConfig_.apiType = IPlugin::ApiType::PUBLIC;
39     needSave_ = false;
40 }
41 
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,MessageParcel & reply,HandlePolicyData & policyData,int32_t userId)42 ErrCode UserCertPlugin::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
43     HandlePolicyData &policyData, int32_t userId)
44 {
45     uint32_t typeCode = FUNC_TO_OPERATE(funcCode);
46     FuncOperateType type = FuncCodeUtils::ConvertOperateType(typeCode);
47     if (type == FuncOperateType::SET) {
48         return InstallUserCert(data, reply);
49     } else if (type == FuncOperateType::REMOVE) {
50         return UninstallUserCert(data, reply);
51     }
52     return EdmReturnErrCode::PARAM_ERROR;
53 }
54 
InstallUserCert(MessageParcel & data,MessageParcel & reply)55 ErrCode UserCertPlugin::InstallUserCert(MessageParcel &data, MessageParcel &reply)
56 {
57     std::vector<uint8_t> certArray;
58     data.ReadUInt8Vector(&certArray);
59     std::string alias = data.ReadString();
60     if (alias.length() >= MAX_ALIAS_LEN || alias.length() == 0) {
61         EDMLOGE("InstallUserCert alias length error");
62         return EdmReturnErrCode::PARAM_ERROR;
63     }
64 
65     uint8_t *ptr = certArray.data();
66     CmBlob certCmBlob = {certArray.size(), ptr};
67 
68     uint8_t arr[MAX_ALIAS_LEN] = {0};
69     std::copy(alias.begin(), alias.end(), std::begin(arr));
70     CmBlob aliasCmBlob = {sizeof(arr), arr};
71 
72     uint8_t uriBuf[MAX_URI_LEN] = {0};
73     CmBlob certUri = {sizeof(uriBuf), uriBuf};
74 
75     int32_t ret = CmInstallUserTrustedCert(&certCmBlob, &aliasCmBlob, &certUri);
76     EDMLOGD("UserCertPlugin::CmInstallUserTrustedCert : %{public}d.", ret);
77     if (FAILED(ret)) {
78         reply.WriteInt32(EdmReturnErrCode::MANAGED_CERTIFICATE_FAILED);
79         reply.WriteInt32(ret);
80     } else {
81         reply.WriteInt32(ERR_OK);
82         std::string result = std::string(reinterpret_cast<char *>(certUri.data), certUri.size);
83         reply.WriteString(result);
84     }
85     return ERR_OK;
86 }
87 
UninstallUserCert(MessageParcel & data,MessageParcel & reply)88 ErrCode UserCertPlugin::UninstallUserCert(MessageParcel &data, MessageParcel &reply)
89 {
90     std::string certUri = data.ReadString();
91     if (certUri.length() >= MAX_CERT_URI_LEN || certUri.length() == 0) {
92         EDMLOGE("UninstallUserCert alias length error");
93         return EdmReturnErrCode::PARAM_ERROR;
94     }
95 
96     uint8_t arr[MAX_CERT_URI_LEN] = {0};
97     std::copy(certUri.begin(), certUri.end(), std::begin(arr));
98     CmBlob aliasCmBlob = {sizeof(arr), arr};
99 
100     int32_t ret = CmUninstallUserTrustedCert(&aliasCmBlob);
101     if (FAILED(ret)) {
102         reply.WriteInt32(EdmReturnErrCode::MANAGED_CERTIFICATE_FAILED);
103         reply.WriteInt32(ret);
104     } else {
105         reply.WriteInt32(ERR_OK);
106     }
107     return ERR_OK;
108 }
109 } // namespace EDM
110 } // namespace OHOS
111