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