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 if (ret != HKS_SUCCESS) {
75 HKS_LOG_E("get public key from x509 format failed, ret = %" LOG_PUBLIC "d", ret);
76 return ret;
77 }
78
79 ret = HksClientAgreeKey(paramSet, privateKey, &publicKey, agreedKey);
80 (void)memset_s(publicKey.data, publicKey.size, 0, publicKey.size);
81 HKS_FREE_BLOB(publicKey);
82 return ret;
83 }
84 #endif
85
86 #ifdef HKS_SUPPORT_API_EXPORT
HksExportPublicKeyAdapter(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,struct HksBlob * key)87 int32_t HksExportPublicKeyAdapter(const struct HksBlob *keyAlias,
88 const struct HksParamSet *paramSet, struct HksBlob *key)
89 {
90 uint8_t *buffer = (uint8_t *)HksMalloc(MAX_KEY_SIZE);
91 HKS_IF_NULL_LOGE_RETURN(buffer, HKS_ERROR_MALLOC_FAIL, "malloc failed")
92 (void)memset_s(buffer, MAX_KEY_SIZE, 0, MAX_KEY_SIZE);
93 struct HksBlob publicKey = { MAX_KEY_SIZE, buffer };
94
95 int32_t ret = HksClientExportPublicKey(keyAlias, paramSet, &publicKey);
96 if (ret == HKS_SUCCESS) {
97 struct HksBlob x509Key = { 0, NULL };
98 ret = TranslateToX509PublicKey(&publicKey, &x509Key);
99 if (ret != HKS_SUCCESS) {
100 HKS_FREE(buffer);
101 return ret;
102 }
103
104 if ((CheckBlob(key) != HKS_SUCCESS) || (memcpy_s(key->data, key->size, x509Key.data, x509Key.size) != EOK)) {
105 ret = HKS_ERROR_INSUFFICIENT_DATA;
106 HKS_LOG_E("x509 format memcpy failed");
107 } else {
108 key->size = x509Key.size;
109 }
110
111 HKS_FREE_BLOB(x509Key);
112 }
113 HKS_FREE_BLOB(publicKey);
114 return ret;
115 }
116 #endif
117