1 /*
2 * Copyright (c) 2020-2024 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 "hks_client_ipc.h"
17 #include "hks_client_service.h"
18 #include "hks_get_process_info.h"
19 #include "hks_log.h"
20 #include "hks_param.h"
21 #include "hks_template.h"
22
23 #define HKS_PROCESS_INFO_LEN 128
24 #define HKS_MAX_DIRENT_FILE_LEN 128
25
GetProcessInfo(const struct HksParamSet * paramSet,char ** processName,char ** userId)26 static int32_t GetProcessInfo(const struct HksParamSet *paramSet, char **processName, char **userId)
27 {
28 (void)paramSet;
29 #ifdef HKS_ENABLE_LITE_HAP
30 struct HksParam *bundleNameParam = NULL;
31 if (paramSet != NULL && HksGetParam(paramSet, HKS_TAG_BUNDLE_NAME, &bundleNameParam) == HKS_SUCCESS) {
32 // the end of bundleNameParam->blob.data is \0 and it's considered in blob.size
33 *processName = (char *)bundleNameParam->blob.data;
34 } else {
35 #endif
36 HKS_IF_NOT_SUCC_LOGE_RETURN(HksGetProcessName(processName), HKS_ERROR_INTERNAL_ERROR, "get process name failed")
37 #ifdef HKS_ENABLE_LITE_HAP
38 }
39 #endif
40 HKS_IF_NOT_SUCC_LOGE_RETURN(HksGetUserId(userId), HKS_ERROR_INTERNAL_ERROR, "get user id failed")
41 return HKS_SUCCESS;
42 }
43
44 #ifndef _CUT_AUTHENTICATE_
HksClientInitialize(void)45 int32_t HksClientInitialize(void)
46 {
47 return HksServiceInitialize();
48 }
49
HksClientRefreshKeyInfo(void)50 int32_t HksClientRefreshKeyInfo(void)
51 {
52 char *processName = NULL;
53 HKS_IF_NOT_SUCC_LOGE_RETURN(HksGetProcessName(&processName), HKS_ERROR_INTERNAL_ERROR, "get process name failed")
54 struct HksBlob processNameBlob = { strlen(processName), (uint8_t *)processName };
55 return HksServiceRefreshKeyInfo(&processNameBlob);
56 }
57
HksClientGenerateKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,struct HksParamSet * paramSetOut)58 int32_t HksClientGenerateKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn,
59 struct HksParamSet *paramSetOut)
60 {
61 (void)paramSetOut;
62 char *processName = NULL;
63 char *userId = NULL;
64 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSetIn, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
65 "get process info failed")
66
67 struct HksProcessInfo processInfo = {
68 { strlen(userId), (uint8_t *)userId },
69 { strlen(processName), (uint8_t *)processName },
70 0,
71 0,
72 0
73 };
74 return HksServiceGenerateKey(&processInfo, keyAlias, paramSetIn, NULL);
75 }
76
HksClientImportKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,const struct HksBlob * key)77 int32_t HksClientImportKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
78 const struct HksBlob *key)
79 {
80 char *processName = NULL;
81 char *userId = NULL;
82 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
83 "get process info failed")
84
85 struct HksProcessInfo processInfo = {
86 { strlen(userId), (uint8_t *)userId },
87 { strlen(processName), (uint8_t *)processName },
88 0,
89 0,
90 0
91 };
92 return HksServiceImportKey(&processInfo, keyAlias, paramSet, key);
93 }
94
HksClientImportWrappedKey(const struct HksBlob * keyAlias,const struct HksBlob * wrappingKeyAlias,const struct HksParamSet * paramSet,const struct HksBlob * wrappedKeyData)95 int32_t HksClientImportWrappedKey(const struct HksBlob *keyAlias, const struct HksBlob *wrappingKeyAlias,
96 const struct HksParamSet *paramSet, const struct HksBlob *wrappedKeyData)
97 {
98 char *processName = NULL;
99 char *userId = NULL;
100 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
101 "get process info failed")
102
103 struct HksProcessInfo processInfo = {
104 { strlen(userId), (uint8_t *)userId },
105 { strlen(processName), (uint8_t *)processName },
106 0,
107 0,
108 0
109 };
110
111 return HksServiceImportWrappedKey(&processInfo, keyAlias, wrappingKeyAlias, paramSet, wrappedKeyData);
112 }
113
HksClientExportPublicKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,struct HksBlob * key)114 int32_t HksClientExportPublicKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
115 struct HksBlob *key)
116 {
117 char *processName = NULL;
118 char *userId = NULL;
119 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
120 "get process info failed")
121
122 struct HksProcessInfo processInfo = {
123 { strlen(userId), (uint8_t *)userId },
124 { strlen(processName), (uint8_t *)processName },
125 0,
126 0,
127 0
128 };
129 return HksServiceExportPublicKey(&processInfo, keyAlias, paramSet, key);
130 }
131
HksClientDeleteKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet)132 int32_t HksClientDeleteKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet)
133 {
134 char *processName = NULL;
135 char *userId = NULL;
136 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
137 "get process info failed")
138
139 struct HksProcessInfo processInfo = {
140 { strlen(userId), (uint8_t *)userId },
141 { strlen(processName), (uint8_t *)processName },
142 0,
143 0,
144 0
145 };
146 return HksServiceDeleteKey(&processInfo, keyAlias, paramSet);
147 }
148
HksClientGetKeyParamSet(const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,struct HksParamSet * paramSetOut)149 int32_t HksClientGetKeyParamSet(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn,
150 struct HksParamSet *paramSetOut)
151 {
152 char *processName = NULL;
153 char *userId = NULL;
154 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSetIn, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
155 "get process info failed")
156
157 struct HksProcessInfo processInfo = {
158 { strlen(userId), (uint8_t *)userId },
159 { strlen(processName), (uint8_t *)processName },
160 0,
161 0,
162 0
163 };
164 return HksServiceGetKeyParamSet(&processInfo, keyAlias, paramSetIn, paramSetOut);
165 }
166
HksClientKeyExist(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet)167 int32_t HksClientKeyExist(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet)
168 {
169 char *processName = NULL;
170 char *userId = NULL;
171 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
172 "get process info failed")
173
174 struct HksProcessInfo processInfo = {
175 { strlen(userId), (uint8_t *)userId },
176 { strlen(processName), (uint8_t *)processName },
177 0,
178 0,
179 0
180 };
181 return HksServiceKeyExist(&processInfo, keyAlias, paramSet);
182 }
183
HksClientSign(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * signature)184 int32_t HksClientSign(const struct HksBlob *key, const struct HksParamSet *paramSet,
185 const struct HksBlob *srcData, struct HksBlob *signature)
186 {
187 char *processName = NULL;
188 char *userId = NULL;
189 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
190 "get process info failed")
191
192 struct HksProcessInfo processInfo = {
193 { strlen(userId), (uint8_t *)userId },
194 { strlen(processName), (uint8_t *)processName },
195 0,
196 0,
197 0
198 };
199 return HksServiceSign(&processInfo, key, paramSet, srcData, signature);
200 }
201
HksClientVerify(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,const struct HksBlob * signature)202 int32_t HksClientVerify(const struct HksBlob *key, const struct HksParamSet *paramSet,
203 const struct HksBlob *srcData, const struct HksBlob *signature)
204 {
205 char *processName = NULL;
206 char *userId = NULL;
207 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
208 "get process info failed")
209
210 struct HksProcessInfo processInfo = {
211 { strlen(userId), (uint8_t *)userId },
212 { strlen(processName), (uint8_t *)processName },
213 0,
214 0,
215 0
216 };
217 return HksServiceVerify(&processInfo, key, paramSet, srcData, signature);
218 }
219
HksClientEncrypt(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * plainText,struct HksBlob * cipherText)220 int32_t HksClientEncrypt(const struct HksBlob *key, const struct HksParamSet *paramSet,
221 const struct HksBlob *plainText, struct HksBlob *cipherText)
222 {
223 char *processName = NULL;
224 char *userId = NULL;
225 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
226 "get process info failed")
227
228 struct HksProcessInfo processInfo = {
229 { strlen(userId), (uint8_t *)userId },
230 { strlen(processName), (uint8_t *)processName },
231 0,
232 0,
233 0
234 };
235 return HksServiceEncrypt(&processInfo, key, paramSet, plainText, cipherText);
236 }
237
HksClientDecrypt(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * cipherText,struct HksBlob * plainText)238 int32_t HksClientDecrypt(const struct HksBlob *key, const struct HksParamSet *paramSet,
239 const struct HksBlob *cipherText, struct HksBlob *plainText)
240 {
241 char *processName = NULL;
242 char *userId = NULL;
243 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
244 "get process info failed")
245
246 struct HksProcessInfo processInfo = {
247 { strlen(userId), (uint8_t *)userId },
248 { strlen(processName), (uint8_t *)processName },
249 0,
250 0,
251 0
252 };
253 return HksServiceDecrypt(&processInfo, key, paramSet, cipherText, plainText);
254 }
255
HksClientAgreeKey(const struct HksParamSet * paramSet,const struct HksBlob * privateKey,const struct HksBlob * peerPublicKey,struct HksBlob * agreedKey)256 int32_t HksClientAgreeKey(const struct HksParamSet *paramSet, const struct HksBlob *privateKey,
257 const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey)
258 {
259 char *processName = NULL;
260 char *userId = NULL;
261 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
262 "get process info failed")
263
264 struct HksProcessInfo processInfo = {
265 { strlen(userId), (uint8_t *)userId },
266 { strlen(processName), (uint8_t *)processName },
267 0,
268 0,
269 0
270 };
271 return HksServiceAgreeKey(&processInfo, paramSet, privateKey, peerPublicKey, agreedKey);
272 }
273
HksClientDeriveKey(const struct HksParamSet * paramSet,const struct HksBlob * mainKey,struct HksBlob * derivedKey)274 int32_t HksClientDeriveKey(const struct HksParamSet *paramSet, const struct HksBlob *mainKey,
275 struct HksBlob *derivedKey)
276 {
277 char *processName = NULL;
278 char *userId = NULL;
279 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
280 "get process info failed")
281
282 struct HksProcessInfo processInfo = {
283 { strlen(userId), (uint8_t *)userId },
284 { strlen(processName), (uint8_t *)processName },
285 0,
286 0,
287 0
288 };
289 return HksServiceDeriveKey(&processInfo, paramSet, mainKey, derivedKey);
290 }
291
HksClientMac(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * mac)292 int32_t HksClientMac(const struct HksBlob *key, const struct HksParamSet *paramSet, const struct HksBlob *srcData,
293 struct HksBlob *mac)
294 {
295 char *processName = NULL;
296 char *userId = NULL;
297 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
298 "get process info failed")
299
300 struct HksProcessInfo processInfo = {
301 { strlen(userId), (uint8_t *)userId },
302 { strlen(processName), (uint8_t *)processName },
303 0,
304 0,
305 0
306 };
307 return HksServiceMac(&processInfo, key, paramSet, srcData, mac);
308 }
309
HksClientGetKeyInfoList(const struct HksParamSet * paramSet,struct HksKeyInfo * keyInfoList,uint32_t * listCount)310 int32_t HksClientGetKeyInfoList(const struct HksParamSet *paramSet, struct HksKeyInfo *keyInfoList,
311 uint32_t *listCount)
312 {
313 char *processName = NULL;
314 char *userId = NULL;
315 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(NULL, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
316 "get process info failed")
317
318 struct HksProcessInfo processInfo = {
319 { strlen(userId), (uint8_t *)userId },
320 { strlen(processName), (uint8_t *)processName },
321 0,
322 0,
323 0
324 };
325 return HksServiceGetKeyInfoList(&processInfo, paramSet, keyInfoList, listCount);
326 }
327
HksClientAttestKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,struct HksCertChain * certChain,bool needAnonCertChain)328 int32_t HksClientAttestKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
329 struct HksCertChain *certChain, bool needAnonCertChain)
330 {
331 (void)keyAlias;
332 (void)paramSet;
333 (void)certChain;
334 (void)needAnonCertChain;
335 return HKS_ERROR_NOT_SUPPORTED;
336 }
337
HksClientInit(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,struct HksBlob * handle,struct HksBlob * token)338 int32_t HksClientInit(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
339 struct HksBlob *handle, struct HksBlob *token)
340 {
341 char *processName = NULL;
342 char *userId = NULL;
343 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
344 "get process info failed")
345
346 struct HksProcessInfo processInfo = {
347 { strlen(userId), (uint8_t *)userId },
348 { strlen(processName), (uint8_t *)processName },
349 0,
350 0,
351 0
352 };
353
354 struct HksBlob tokenTmp = { 0, NULL };
355 if ((token != NULL) && (token->size != 0)) {
356 tokenTmp.size = token->size;
357 tokenTmp.data = token->data;
358 }
359 int32_t ret = HksServiceInit(&processInfo, keyAlias, paramSet, handle, &tokenTmp);
360 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "service init failed, ret = %" LOG_PUBLIC "d", ret)
361
362 if ((token != NULL) && (token->size != 0)) {
363 token->size = tokenTmp.size;
364 }
365 return ret;
366 }
367
HksClientUpdate(const struct HksBlob * handle,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData)368 int32_t HksClientUpdate(const struct HksBlob *handle, const struct HksParamSet *paramSet,
369 const struct HksBlob *inData, struct HksBlob *outData)
370 {
371 char *processName = NULL;
372 char *userId = NULL;
373 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
374 "get process info failed")
375
376 struct HksProcessInfo processInfo = {
377 { strlen(userId), (uint8_t *)userId },
378 { strlen(processName), (uint8_t *)processName },
379 0,
380 0,
381 0
382 };
383 return HksServiceUpdate(handle, &processInfo, paramSet, inData, outData);
384 }
385
HksClientFinish(const struct HksBlob * handle,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData)386 int32_t HksClientFinish(const struct HksBlob *handle, const struct HksParamSet *paramSet,
387 const struct HksBlob *inData, struct HksBlob *outData)
388 {
389 char *processName = NULL;
390 char *userId = NULL;
391 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
392 "get process info failed")
393
394 struct HksProcessInfo processInfo = {
395 { strlen(userId), (uint8_t *)userId },
396 { strlen(processName), (uint8_t *)processName },
397 0,
398 0,
399 0
400 };
401 return HksServiceFinish(handle, &processInfo, paramSet, inData, outData);
402 }
403
HksClientAbort(const struct HksBlob * handle,const struct HksParamSet * paramSet)404 int32_t HksClientAbort(const struct HksBlob *handle, const struct HksParamSet *paramSet)
405 {
406 char *processName = NULL;
407 char *userId = NULL;
408 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
409 "get process info failed")
410
411 struct HksProcessInfo processInfo = {
412 { strlen(userId), (uint8_t *)userId },
413 { strlen(processName), (uint8_t *)processName },
414 0,
415 0,
416 0
417 };
418 return HksServiceAbort(handle, &processInfo, paramSet);
419 }
420 #endif
421
HksClientGenerateRandom(struct HksBlob * random,const struct HksParamSet * paramSet)422 int32_t HksClientGenerateRandom(struct HksBlob *random, const struct HksParamSet *paramSet)
423 {
424 (void)paramSet;
425 char *processName = NULL;
426 char *userId = NULL;
427 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(paramSet, &processName, &userId), HKS_ERROR_INTERNAL_ERROR,
428 "get process info failed")
429
430 struct HksProcessInfo processInfo = {
431 { strlen(userId), (uint8_t *)userId },
432 { strlen(processName), (uint8_t *)processName },
433 0,
434 0,
435 0
436 };
437 return HksServiceGenerateRandom(&processInfo, random);
438 }
439
440 #ifdef HKS_SUPPORT_CHIPSET_PLATFORM_DECRYPT
HksClientExportChipsetPlatformPublicKey(const struct HksBlob * salt,enum HksChipsetPlatformDecryptScene scene,struct HksBlob * publicKey)441 int32_t HksClientExportChipsetPlatformPublicKey(const struct HksBlob *salt,
442 enum HksChipsetPlatformDecryptScene scene, struct HksBlob *publicKey)
443 {
444 return HksServiceExportChipsetPlatformPublicKey(salt, scene, publicKey);
445 }
446 #endif
447
HksClientListAliases(const struct HksParamSet * paramSet,struct HksKeyAliasSet ** outData)448 int32_t HksClientListAliases(const struct HksParamSet *paramSet, struct HksKeyAliasSet **outData)
449 {
450 char *tempProcessName = NULL;
451 char *tempUserId = NULL;
452 HKS_IF_NOT_SUCC_LOGE_RETURN(GetProcessInfo(NULL, &tempProcessName, &tempUserId), HKS_ERROR_INTERNAL_ERROR,
453 "get process info failed")
454
455 struct HksProcessInfo processInfo = {
456 { strlen(tempUserId), (uint8_t *)tempUserId },
457 { strlen(tempProcessName), (uint8_t *)tempProcessName },
458 0,
459 0,
460 0
461 };
462 return HksServiceListAliases(&processInfo, paramSet, outData);
463 }
464
HksClientRenameKeyAlias(const struct HksBlob * oldKeyAlias,const struct HksParamSet * paramSet,const struct HksBlob * newKeyAlias)465 int32_t HksClientRenameKeyAlias(const struct HksBlob *oldKeyAlias, const struct HksParamSet *paramSet,
466 const struct HksBlob *newKeyAlias)
467 {
468 (void)oldKeyAlias;
469 (void)paramSet;
470 (void)newKeyAlias;
471 return HKS_ERROR_NOT_SUPPORTED;
472 }
473
HksClientChangeStorageLevel(const struct HksBlob * keyAlias,const struct HksParamSet * srcParamSet,const struct HksParamSet * destParamSet)474 int32_t HksClientChangeStorageLevel(const struct HksBlob *keyAlias, const struct HksParamSet *srcParamSet,
475 const struct HksParamSet *destParamSet)
476 {
477 (void)keyAlias;
478 (void)srcParamSet;
479 (void)destParamSet;
480 return HKS_ERROR_NOT_SUPPORTED;
481 }