• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "attest_type.h"
17 #include "attest_utils_log.h"
18 #include "attest_utils_timer.h"
19 #include "attest_service.h"
20 #include "attest_entry.h"
21 
22 static ATTEST_TIMER_ID g_ProcAttestTimerId = NULL;
23 
24 #ifdef __LITEOS_M__
25 
26 static osThreadId_t g_AttestTaskId = NULL;
27 typedef void(*AttestTaskCallback)(void);
28 // L0启动
CreateAttestThread(void (* run)(void *),void * argv,const char * name,osThreadId_t * serverTaskId)29 static int CreateAttestThread(void(*run)(void *), void *argv, const char *name, osThreadId_t *serverTaskId)
30 {
31     osThreadAttr_t attr = {0};
32     attr.stack_size = LITEOS_M_STACK_SIZE;
33     attr.priority = osPriorityNormal;
34     attr.name = name;
35     *serverTaskId = osThreadNew((osThreadFunc_t)run, argv, &attr);
36     if (*serverTaskId == NULL) {
37         ATTEST_LOG_ERROR("[CreateAttestThread] osThreadNew fail.");
38         return ATTEST_ERR;
39     }
40     return ATTEST_OK;
41 }
42 
AttestTaskThread(void * argv)43 static void AttestTaskThread(void *argv)
44 {
45     AttestTaskCallback cb = (AttestTaskCallback)argv;
46     cb();
47     return;
48 }
49 
AttestAuthCallBack(void * argv)50 static void AttestAuthCallBack(void *argv)
51 {
52     (void)argv;
53     if (g_AttestTaskId != NULL) {
54         const char *pthreadName = osThreadGetName(g_AttestTaskId);
55         if ((pthreadName != NULL) && (strcmp(pthreadName, ATTEST_CALLBACK_THREAD_NAME) == 0)) {
56             osThreadTerminate(g_AttestTaskId);
57             ATTEST_LOG_ERROR("[AttestAuthCallBack] osThreadTerminate");
58         }
59         g_AttestTaskId = NULL;
60     }
61     int ret = CreateAttestThread(AttestTaskThread,
62         (void *)ProcAttest,
63         ATTEST_CALLBACK_THREAD_NAME,
64         &g_AttestTaskId);
65     if (ret != ATTEST_OK) {
66         ATTEST_LOG_ERROR("[AttestAuthCallBack] CreateAttestThread return failed");
67     }
68     return;
69 }
70 #else
AttestAuthCallBack(void * argv)71 static void AttestAuthCallBack(void *argv)
72 {
73     (void)argv;
74     int32_t ret = ProcAttest();
75     if (ret != ATTEST_OK) {
76         ATTEST_LOG_ERROR("[AttestAuthCallBack] Proc failed ret = %d.", ret);
77     }
78     return;
79 }
80 #endif
81 
AttestTask(void)82 int32_t AttestTask(void)
83 {
84     ATTEST_LOG_INFO("[AttestTask] Begin.");
85     // 执行主流程代码
86     int32_t ret = ProcAttest();
87     if (ret != ATTEST_OK) {
88         ATTEST_LOG_ERROR("[AttestTask] Proc failed ret = %d.", ret);
89     }
90 
91     // 创建主流程定时器
92     ret = AttestCreateTimerTask(ATTEST_TIMER_TYPE_PERIOD,
93         EXPIRED_INTERVAL,
94         &AttestAuthCallBack,
95         NULL,
96         &g_ProcAttestTimerId);
97     if (ret != ATTEST_OK) {
98         ATTEST_LOG_ERROR("[AttestTask] Create Periodic TimerTask return ret = %d.", ret);
99     }
100     ATTEST_LOG_INFO("[AttestTask] End.");
101     return ret;
102 }
103 
CopyAttestResult(int32_t * resultArray,AttestResultInfo * attestResultInfo)104 static int32_t CopyAttestResult(int32_t *resultArray, AttestResultInfo *attestResultInfo)
105 {
106     if (resultArray == NULL) {
107         return DEVATTEST_FAIL;
108     }
109     int32_t *head = resultArray;
110     attestResultInfo->authResult = *head;
111     head++;
112     attestResultInfo->softwareResult = *head;
113     for (int i = 0; i < SOFTWARE_RESULT_DETAIL_SIZE; i++) {
114         attestResultInfo->softwareResultDetail[i] = *(++head);
115     }
116     return DEVATTEST_SUCCESS;
117 }
118 
EntryGetAttestStatus(AttestResultInfo * attestResultInfo)119 int32_t EntryGetAttestStatus(AttestResultInfo* attestResultInfo)
120 {
121     if (attestResultInfo == NULL) {
122         return DEVATTEST_FAIL;
123     }
124     int32_t resultArraySize = MAX_ATTEST_RESULT_SIZE * sizeof(int32_t);
125     int32_t *resultArray = (int32_t *)malloc(resultArraySize);
126     if (resultArray == NULL) {
127         ATTEST_LOG_ERROR("malloc resultArray failed");
128         return DEVATTEST_FAIL;
129     }
130     (void)memset_s(resultArray, resultArraySize, 0, resultArraySize);
131     int32_t ticketLength = 0;
132     char* ticketStr = NULL;
133     int32_t ret = DEVATTEST_SUCCESS;
134     do {
135         ret = QueryAttestStatus(&resultArray, MAX_ATTEST_RESULT_SIZE, &ticketStr, &ticketLength);
136         if (ret != DEVATTEST_SUCCESS) {
137             ATTEST_LOG_ERROR("QueryAttest failed");
138             break;
139         }
140         ret = CopyAttestResult(resultArray,  attestResultInfo);
141         if (ret != DEVATTEST_SUCCESS) {
142             ATTEST_LOG_ERROR("copy attest result failed");
143             break;
144         }
145         attestResultInfo->ticketLength = ticketLength;
146         attestResultInfo->ticket = ticketStr;
147     } while (0);
148     free(resultArray);
149     resultArray = NULL;
150     ATTEST_LOG_INFO("GetAttestStatus end success");
151     return ret;
152 }