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 #ifndef __LITEOS_M__
17 #include <securec.h>
18 #include "syscap_interface.h"
19 #include "attest_utils.h"
20 #include "attest_utils_log.h"
21 #include "attest_service_device.h"
22
23 #define PCID_STRING_LEN 64
24
MergePcid(char * osPcid,int32_t osPcidLen,char * privatePcid,int32_t privatePcidLen,char ** output)25 static int32_t MergePcid(char *osPcid, int32_t osPcidLen, char *privatePcid, int32_t privatePcidLen, char **output)
26 {
27 if (output == NULL || osPcid == NULL || osPcidLen == 0) {
28 ATTEST_LOG_ERROR("[MergePcid] Invalid parameter.");
29 return ATTEST_ERR;
30 }
31
32 int32_t pcidLen = osPcidLen + privatePcidLen;
33 char *pcidBuff = (char *)ATTEST_MEM_MALLOC(pcidLen);
34 if (pcidBuff == NULL) {
35 ATTEST_LOG_ERROR("[MergePcid] Failed to malloc.");
36 return ATTEST_ERR;
37 }
38
39 if (memcpy_s(pcidBuff, pcidLen, osPcid, osPcidLen) != 0) {
40 ATTEST_LOG_ERROR("[MergePcid] Failed to memcpy osSyscaps.");
41 ATTEST_MEM_FREE(pcidBuff);
42 return ATTEST_ERR;
43 }
44
45 if ((privatePcidLen > 0 && privatePcid != NULL) &&
46 (memcpy_s(pcidBuff, pcidLen, privatePcid, privatePcidLen) != 0)) {
47 ATTEST_LOG_ERROR("[MergePcid] Failed to memcpy privateSyscaps.");
48 ATTEST_MEM_FREE(pcidBuff);
49 return ATTEST_ERR;
50 }
51
52 *output = pcidBuff;
53 return ATTEST_OK;
54 }
55
EncodePcid(char * buff,int32_t bufLen,char ** output)56 static int32_t EncodePcid(char *buff, int32_t bufLen, char **output)
57 {
58 if (output == NULL || buff == NULL || bufLen == 0) {
59 ATTEST_LOG_ERROR("[EncodePcid] Invalid parameter.");
60 return ATTEST_ERR;
61 }
62
63 char *pcidSha256 = (char *)ATTEST_MEM_MALLOC(PCID_STRING_LEN + 1);
64 if (pcidSha256 == NULL) {
65 ATTEST_LOG_ERROR("[EncodePcid] Failed to malloc.");
66 return ATTEST_ERR;
67 }
68
69 int32_t ret = Sha256Value((const uint8_t *)buff, bufLen, pcidSha256, PCID_STRING_LEN + 1);
70 if (ret != ATTEST_OK) {
71 ATTEST_LOG_ERROR("[EncodePcid] Failed to encode.");
72 ATTEST_MEM_FREE(pcidSha256);
73 return ATTEST_ERR;
74 }
75
76 *output = pcidSha256;
77 return ATTEST_OK;
78 }
79
GetPcid(void)80 char* GetPcid(void)
81 {
82 char osSyscaps[PCID_MAIN_BYTES] = {0};
83 if (!EncodeOsSyscap(osSyscaps, PCID_MAIN_BYTES)) {
84 ATTEST_LOG_ERROR("[GetPcid] EncodeOsSyscap failed");
85 return NULL;
86 }
87
88 char *privateSyscaps = NULL;
89 int32_t privatePcidLen = 0;
90 if (!EncodePrivateSyscap(&privateSyscaps, &privatePcidLen)) {
91 ATTEST_LOG_ERROR("[GetPcid] EncodePrivateSyscap failed");
92 return NULL;
93 }
94
95 // Merge OsSyscap and PrivateSyscap
96 char *pcidBuff = NULL;
97 int32_t ret = MergePcid(osSyscaps, PCID_MAIN_BYTES, privateSyscaps, privatePcidLen, &pcidBuff);
98 if (ret != ATTEST_OK || pcidBuff == NULL) {
99 ATTEST_LOG_ERROR("[GetPcid] Failed to merge Pcid.");
100 return NULL;
101 }
102
103 // SHA256转换
104 char *pcidSha256 = NULL;
105 ret = EncodePcid(pcidBuff, PCID_MAIN_BYTES + privatePcidLen, &pcidSha256);
106 if (ret != ATTEST_OK || pcidSha256 == NULL) {
107 ATTEST_LOG_ERROR("[GetPcid] Failed to SHA256.");
108 ATTEST_MEM_FREE(pcidBuff);
109 return NULL;
110 }
111
112 ATTEST_MEM_FREE(pcidBuff);
113 return pcidSha256;
114 }
115 #endif
116