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 CERT_MANAGER_STATUS_H
17 #define CERT_MANAGER_STATUS_H
18 
19 #include "cert_manager_mem.h"
20 
21 #include "rbtree.h"
22 
23 #include "cm_type.h"
24 
25 #define  CERT_STATUS_ENANLED           ((uint32_t) 0)
26 #define  CERT_STATUS_DISABLED          ((uint32_t) 1)
27 #define  CERT_STATUS_MAX                CERT_STATUS_DISABLED
28 #define  CERT_STATUS_INVALID            ((uint32_t)(CERT_STATUS_MAX + 1))
29 
30 // integrity protection key for internal use only.
31 #define CM_INTEGRITY_KEY_URI        "oh:o=ik;t=mk;a=cm"
32 #define CM_INTEGRITY_TAG_LEN        ((uint32_t) 32)
33 #define CM_INTEGRITY_SALT_LEN       ((uint32_t) 32)
34 #define VERSION_1                   ((uint32_t) 0)
35 #define VERSION_1                   ((uint32_t) 0)
36 
37 #define DECODE_UINT32(_b) (uint32_t)(((_b)[0] << 24) | ((_b)[1] << 16) | ((_b)[2] << 8) | (_b)[3])
38 #define ENCODE_UINT32(_b, _i) do { (_b)[0] = ((_i) >> 24) & 0xff; (_b)[1] = ((_i) >> 16) & 0xff; \
39     (_b)[2] = ((_i) >> 8) & 0xff; (_b)[3] = (_i) & 0xff; } while (0)
40 
41 #define  CERT_STATUS_DIR                    "/data/service/el1/public/cert_manager_service/status"
42 #define  CERT_STATUS_SYSTEM_STORE           "system"
43 #define  CERT_STATUS_USER_STORE             "user"
44 #define  CERT_STATUS_APPLICATION_STORE      "app"
45 
46 #define CM_ERROR(rc)  (int32_t) (rc)
47 
48 #define ASSERT_ARGS(c) if (!(c)) { CM_LOG_W("Invalid args: %s\n", #c); return CMR_ERROR_INVALID_ARGUMENT; }
49 #define ASSERT_FUNC(f) if (CMR_OK != (f)) { CM_LOG_W("Failed: %s\n", #f); return CMR_ERROR; }
50 
51 #define ASSERT_CM_CALL(f) do {int32_t _rc = (f); if ((_rc) != CM_SUCCESS) { return CM_ERROR((_rc)); }} while (0)
52 
53 #define TRY_FUNC(f, rc) do { \
54     (rc) = (f); if ((rc)) { CM_LOG_W("Failed: %s, %d\n", #f, (rc)); goto finally; }} while (0)
55 
56 #define  FREE_PTR(p)   if ((p) != NULL) { CMFree((p)); (p) = NULL; }
57 
58 #define  CM_BLOB(b)   (struct CmBlob) { .size = (b)->size, .data = (uint8_t *) (b)->data }
59 
60 #define  HKS_BLOB(b)   (struct HksBlob) { .size = (b)->size, .data = (uint8_t *) (b)->data }
61 
62 #define TRY_HKS_CALL(f, rc) do {int32_t _rc = (f); if ((_rc) != HKS_SUCCESS) { \
63     CM_LOG_W("Failed: %s, %d\n", #f, (_rc)); (rc) = CM_ERROR((_rc)); goto finally; }} while (0)
64 
65 typedef int (*RbTreeValueEncoder)(RbTreeValue value, uint8_t *buf, uint32_t *size);
66 
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
71 struct CertStatus {
72     uint32_t userId;
73     uint32_t uid;
74     uint32_t status;
75     char *fileName;
76 };
77 
78 int32_t CertManagerStatusInit(void);
79 
80 int32_t SetcertStatus(const struct CmContext *context, const struct CmBlob *certUri,
81     uint32_t store, uint32_t status, uint32_t *stp);
82 
83 int32_t CmSetStatusEnable(const struct CmContext *context, struct CmMutableBlob *pathBlob,
84     const struct CmBlob *certUri, uint32_t store);
85 
86 int32_t CmGetCertStatus(const struct CmContext *context, struct CertFileInfo *cFile,
87     uint32_t store, uint32_t *status);
88 
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 #endif // CERT_MANAGER_STATUS_H