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