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