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 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16
17 #ifdef HKS_CONFIG_FILE
18 #include HKS_CONFIG_FILE
19 #else
20 #include "hks_config.h"
21 #endif
22
23 #include "hks_api_adapter.h"
24
25 #include <stddef.h>
26
27 #include "hks_client_ipc.h"
28 #include "hks_client_service_adapter.h"
29 #include "hks_client_service_adapter_common.h"
30 #include "hks_log.h"
31 #include "hks_mem.h"
32 #include "hks_param.h"
33 #include "hks_template.h"
34 #include "securec.h"
35
36 #ifdef _CUT_AUTHENTICATE_
37 #undef HKS_SUPPORT_API_IMPORT
38 #undef HKS_SUPPORT_API_EXPORT
39 #undef HKS_SUPPORT_API_AGREE_KEY
40 #endif
41
42 #ifdef HKS_SUPPORT_API_IMPORT
HksImportKeyAdapter(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,const struct HksBlob * key)43 int32_t HksImportKeyAdapter(const struct HksBlob *keyAlias,
44 const struct HksParamSet *paramSet, const struct HksBlob *key)
45 {
46 struct HksBlob innerKey = { 0, NULL };
47
48 struct HksParam *importKeyTypeParam = NULL;
49 int32_t ret = HksGetParam(paramSet, HKS_TAG_IMPORT_KEY_TYPE, &importKeyTypeParam);
50 if ((ret == HKS_SUCCESS) &&
51 ((importKeyTypeParam->uint32Param == HKS_KEY_TYPE_PRIVATE_KEY) ||
52 (importKeyTypeParam->uint32Param == HKS_KEY_TYPE_KEY_PAIR))) {
53 ret = CopyToInnerKey(key, &innerKey);
54 } else {
55 ret = GetHksPubKeyInnerFormat(paramSet, key, &innerKey);
56 }
57 if (ret != HKS_SUCCESS) {
58 HKS_LOG_E("translate key to inner format failed, ret = %" LOG_PUBLIC "d", ret);
59 return ret;
60 }
61
62 ret = HksClientImportKey(keyAlias, paramSet, &innerKey);
63 (void)memset_s(innerKey.data, innerKey.size, 0, innerKey.size);
64 HKS_FREE_BLOB(innerKey);
65 return ret;
66 }
67 #endif
68
69 #ifdef HKS_SUPPORT_API_AGREE_KEY
HksAgreeKeyAdapter(const struct HksParamSet * paramSet,const struct HksBlob * privateKey,const struct HksBlob * peerPublicKey,struct HksBlob * agreedKey)70 int32_t HksAgreeKeyAdapter(const struct HksParamSet *paramSet, const struct HksBlob *privateKey,
71 const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey)
72 {
73 struct HksBlob publicKey = { 0, NULL };
74 int32_t ret = GetHksPubKeyInnerFormat(paramSet, peerPublicKey, &publicKey);
75 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get public key from x509 format failed, ret = %" LOG_PUBLIC "d", ret)
76
77 ret = HksClientAgreeKey(paramSet, privateKey, &publicKey, agreedKey);
78 (void)memset_s(publicKey.data, publicKey.size, 0, publicKey.size);
79 HKS_FREE_BLOB(publicKey);
80 return ret;
81 }
82 #endif
83
84 #ifdef HKS_SUPPORT_API_EXPORT
HksExportPublicKeyAdapter(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,struct HksBlob * key)85 int32_t HksExportPublicKeyAdapter(const struct HksBlob *keyAlias,
86 const struct HksParamSet *paramSet, struct HksBlob *key)
87 {
88 uint8_t *buffer = (uint8_t *)HksMalloc(MAX_KEY_SIZE);
89 HKS_IF_NULL_LOGE_RETURN(buffer, HKS_ERROR_MALLOC_FAIL, "malloc failed")
90 (void)memset_s(buffer, MAX_KEY_SIZE, 0, MAX_KEY_SIZE);
91 struct HksBlob publicKey = { MAX_KEY_SIZE, buffer };
92
93 int32_t ret = HksClientExportPublicKey(keyAlias, paramSet, &publicKey);
94 if (ret == HKS_SUCCESS) {
95 struct HksBlob x509Key = { 0, NULL };
96 ret = TranslateToX509PublicKey(&publicKey, &x509Key);
97 if (ret != HKS_SUCCESS) {
98 HKS_FREE(buffer);
99 return ret;
100 }
101
102 if ((CheckBlob(key) != HKS_SUCCESS) || (memcpy_s(key->data, key->size, x509Key.data, x509Key.size) != EOK)) {
103 ret = HKS_ERROR_INSUFFICIENT_DATA;
104 HKS_LOG_E("x509 format memcpy failed");
105 } else {
106 key->size = x509Key.size;
107 }
108
109 HKS_FREE_BLOB(x509Key);
110 }
111 HKS_FREE_BLOB(publicKey);
112 return ret;
113 }
114 #endif
115