1 /*
2  * Copyright (C) 2022 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 #ifndef AUTH_RESOURCE_POOL_H
17 #define AUTH_RESOURCE_POOL_H
18 
19 #include "buffer.h"
20 #include "c_array.h"
21 #include "linked_list.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define PUBLIC_KEY_LEN 32
28 #define INVALID_EXECUTOR_INDEX 0
29 #define CHALLENGE_LEN 32
30 
31 typedef enum ExecutorRole {
32     COLLECTOR = 1,
33     VERIFIER = 2,
34     ALL_IN_ONE = 3,
35 } ExecutorRole;
36 
37 typedef struct ExecutorInfoHal {
38     uint64_t executorIndex;
39     uint32_t authType;
40     uint32_t executorSensorHint;
41     uint32_t executorRole;
42     uint32_t executorMatcher;
43     uint32_t esl;
44     uint32_t maxTemplateAcl;
45     uint8_t pubKey[PUBLIC_KEY_LEN];
46     uint8_t deviceUdid[UDID_LEN];
47 } ExecutorInfoHal;
48 
49 typedef enum ExecutorConditionTag {
50     EXECUTOR_CONDITION_INDEX = 1,
51     EXECUTOR_CONDITION_AUTH_TYPE = 2, // 1 << 1
52     EXECUTOR_CONDITION_SENSOR_HINT = 4, // 1 << 2
53     EXECUTOR_CONDITION_ROLE = 8, // 1 << 3
54     EXECUTOR_CONDITION_MATCHER = 16, // 1 << 4
55     EXECUTOR_CONDITION_UDID = 32, // 1 << 5
56 } ExecutorConditionTag;
57 
58 typedef struct ExecutorCondition {
59     uint64_t conditonFactor;
60     uint64_t executorIndex;
61     uint32_t authType;
62     uint32_t executorSensorHint;
63     uint32_t executorRole;
64     uint32_t executorMatcher;
65     uint8_t deviceUdid[UDID_LEN];
66 } ExecutorCondition;
67 
68 ResultCode InitResourcePool(void);
69 void DestroyResourcePool(void);
70 ResultCode RegisterExecutorToPool(ExecutorInfoHal *executorInfo);
71 ResultCode UnregisterExecutorToPool(uint64_t executorIndex);
72 
73 LinkedList *QueryExecutor(const ExecutorCondition *condition);
74 ResultCode QueryCollecterMatcher(uint32_t authType, uint32_t executorSensorHint, uint32_t *matcher);
75 uint64_t QueryCredentialExecutorIndex(uint32_t authType, uint32_t executorSensorHint);
76 ExecutorInfoHal *CopyExecutorInfo(ExecutorInfoHal *src);
77 
78 void SetExecutorConditionExecutorIndex(ExecutorCondition *condition, uint64_t executorIndex);
79 void SetExecutorConditionAuthType(ExecutorCondition *condition, uint32_t authType);
80 void SetExecutorConditionSensorHint(ExecutorCondition *condition, uint32_t executorSensorHint);
81 void SetExecutorConditionExecutorRole(ExecutorCondition *condition, uint32_t executorRole);
82 void SetExecutorConditionExecutorMatcher(ExecutorCondition *condition, uint32_t executorMatcher);
83 void SetExecutorConditionDeviceUdid(ExecutorCondition *condition, Uint8Array deviceUdid);
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif
90