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 "wifi_cert_utils.h"
17 #include "cert_manager_api.h"
18 #include "securec.h"
19 #include "wifi_log.h"
20 
21 namespace OHOS {
22 namespace Wifi {
23 constexpr int MAX_ALIAS_LEN = 128;
24 
CheckParamters(const std::vector<uint8_t> & certEntry,const std::string & pwd,std::string & alias)25 static bool CheckParamters(const std::vector<uint8_t>& certEntry, const std::string& pwd,
26     std::string& alias)
27 {
28     if (certEntry.size() == 0 || (pwd.size() + 1) > MAX_ALIAS_LEN ||
29             (alias.size() + 1) > MAX_ALIAS_LEN) {
30         LOGE("InstallCert, certEntry.size: %{public}zu, pwd.size: %{public}zu, alias.size: %{public}zu.",
31             certEntry.size(), pwd.size(), alias.size());
32         return false;
33     }
34     return true;
35 }
36 
InstallCert(const std::vector<uint8_t> & certEntry,const std::string & pwd,std::string & alias,std::string & uri)37 int WifiCertUtils::InstallCert(const std::vector<uint8_t>& certEntry, const std::string& pwd,
38     std::string& alias, std::string& uri)
39 {
40     if (!CheckParamters(certEntry, pwd, alias)) {
41         return -1;
42     }
43     struct CmBlob appCert;
44     struct CmBlob appCertPwd;
45     struct CmBlob certAlias;
46     char certPwdBuf[MAX_ALIAS_LEN] = { 0 };
47     char certAliasBuf[MAX_ALIAS_LEN] = { 0 };
48     uint8_t *data = reinterpret_cast<uint8_t*>(malloc(certEntry.size()));
49     if (data == nullptr) {
50         LOGE("InstallCert, malloc return null.");
51         return -1;
52     }
53 
54     if (memcpy_s(data, certEntry.size(), certEntry.data(), certEntry.size()) != EOK) {
55         LOGE("memcpy_s certEntry.data() error.");
56         free(data);
57         data = nullptr;
58         return -1;
59     }
60     if (memcpy_s(certPwdBuf, sizeof(certPwdBuf), pwd.c_str(), pwd.size()) != EOK) {
61         LOGE("memcpy_s pwd.c_str() error.");
62         free(data);
63         data = nullptr;
64         return -1;
65     }
66     if (memcpy_s(certAliasBuf, sizeof(certAliasBuf), alias.c_str(), alias.size()) != EOK) {
67         LOGE("memcpy_s alias.c_str() error.");
68         free(data);
69         data = nullptr;
70         return -1;
71     }
72 
73     appCert.size = certEntry.size();
74     appCert.data = data;
75     appCertPwd.size = strlen(certPwdBuf) + 1;
76     appCertPwd.data = reinterpret_cast<uint8_t*>(certPwdBuf);
77     certAlias.size = strlen(certAliasBuf) + 1;
78     certAlias.data = reinterpret_cast<uint8_t*>(certAliasBuf);
79 
80     uint32_t store = 3;
81     char retUriBuf[MAX_ALIAS_LEN] = { 0 };
82     struct CmBlob keyUri = { sizeof(retUriBuf), reinterpret_cast<uint8_t*>(retUriBuf) };
83     int ret = CmInstallAppCert(&appCert, &appCertPwd, &certAlias, store, &keyUri);
84 
85     free(data);
86     data = nullptr;
87     if (ret == 0) {
88         uri = reinterpret_cast<char*>(keyUri.data);
89     }
90 
91     return ret;
92 }
93 
UninstallCert(std::string & uri)94 int WifiCertUtils::UninstallCert(std::string& uri)
95 {
96     if (uri.size() >= MAX_ALIAS_LEN) {
97         LOGE("UninstallCert, uri.size: %{public}zu.", uri.size());
98         return -1;
99     }
100 
101     uint32_t store = 0;
102     struct CmBlob keyUri;
103     char keyUriBuf[MAX_ALIAS_LEN] = { 0 };
104 
105     if (memcpy_s(keyUriBuf, sizeof(keyUriBuf), uri.c_str(), uri.size()) != EOK) {
106         LOGE("memcpy_s uri.c_str() error.");
107         return -1;
108     }
109     keyUri.size = strlen(keyUriBuf) + 1;
110     keyUri.data = reinterpret_cast<uint8_t*>(keyUriBuf);
111     return CmUninstallAppCert(&keyUri, store);
112 }
113 }
114 }
115