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 <sys/types.h>
17 #include <fcntl.h>
18 #include <cerrno>
19 #include <cstring>
20 #include <unistd.h>
21 #include <sys/ioctl.h>
22 #include <parameters.h>
23 #include <parameter.h>
24 #include "log.h"
25 #include "errcode.h"
26 #include "cert_path.h"
27
28 using namespace OHOS::Security::CodeSign;
29
IoctlCertPathOperation(const CertPathInfo & info,int cmd,const char * operation)30 static int IoctlCertPathOperation(const CertPathInfo &info, int cmd, const char *operation)
31 {
32 int fd = open(CERT_DEVICE_PATH, O_WRONLY);
33 if (fd == -1) {
34 LOG_ERROR(LABEL, "Error opening device, errno = <%{public}d, %{public}s>", errno, strerror(errno));
35 return CS_ERR_FILE_OPEN;
36 }
37
38 int ret = ioctl(fd, cmd, &info);
39 if (ret < 0) {
40 LOG_ERROR(
41 LABEL, "%s cert path ioctl error, errno = <%{public}d, %{public}s>", operation, errno, strerror(errno));
42 close(fd);
43 return ret;
44 }
45
46 close(fd);
47 return CS_SUCCESS;
48 }
49
AddCertPath(const CertPathInfo & info)50 int AddCertPath(const CertPathInfo &info)
51 {
52 return IoctlCertPathOperation(info, ADD_CERT_PATH_CMD, "add");
53 }
54
RemoveCertPath(const CertPathInfo & info)55 int RemoveCertPath(const CertPathInfo &info)
56 {
57 return IoctlCertPathOperation(info, REMOVE_CERT_PATH_CMD, "remove");
58 }
59
IsDeveloperModeOn()60 bool IsDeveloperModeOn()
61 {
62 bool ret = false;
63 if (OHOS::system::GetBoolParameter("const.security.developermode.state", false)) {
64 ret = true;
65 }
66 return ret;
67 }
68
CodeSignGetUdid(char * udid)69 int CodeSignGetUdid(char *udid)
70 {
71 return GetDevUdid(udid, UDID_SIZE);
72 }