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