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 "key_utils.h"
17
18 #include <asm/unistd.h>
19 #include <cerrno>
20 #include <cstring>
21 #include <unistd.h>
22
23 #include "log.h"
24
25 constexpr int KEYCTL_RESTRICT_KEYRING = 29;
26
27 using namespace OHOS::Security::CodeSign;
28
AddKey(const char * type,const char * description,const unsigned char * payload,size_t pLen,KeySerial ringId)29 KeySerial AddKey(
30 const char *type,
31 const char *description,
32 const unsigned char *payload,
33 size_t pLen,
34 KeySerial ringId)
35 {
36 KeySerial ret = syscall(__NR_add_key,
37 type, description, static_cast<const void *>(payload),
38 pLen, ringId);
39 if (ret < 0) {
40 LOG_ERROR(LABEL, "Add certificate failed, errno = <%{public}d, %{public}s>",
41 errno, strerror(errno));
42 }
43 return ret;
44 }
45
KeyctlRestrictKeyring(KeySerial ringId,const char * type,const char * restriction)46 KeySerial KeyctlRestrictKeyring(
47 KeySerial ringId,
48 const char *type,
49 const char *restriction)
50 {
51 KeySerial ret = syscall(__NR_keyctl,
52 KEYCTL_RESTRICT_KEYRING, ringId,
53 type, restriction);
54 if (ret < 0) {
55 LOG_ERROR(LABEL, "Restrict keyring failed, errno = <%{public}d, %{public}s>",
56 errno, strerror(errno));
57 }
58 return ret;
59 }
60