• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 
18 #if defined(HITLS_CRYPTO_CODECSKEY)
19 #include "securec.h"
20 #include "bsl_sal.h"
21 #include "bsl_list.h"
22 #include "sal_file.h"
23 #include "bsl_err_internal.h"
24 #include "crypt_errno.h"
25 #include "crypt_eal_provider.h"
26 #include "crypt_eal_implprovider.h"
27 #include "crypt_eal_codecs.h"
28 #include "crypt_provider.h"
29 #include "crypt_eal_pkey.h"
30 #include "bsl_types.h"
31 #include "crypt_types.h"
32 #include "crypt_utils.h"
33 #include "eal_pkey.h"
34 #include "crypt_encode_decode_local.h"
35 #include "crypt_encode_decode_key.h"
36 
37 #if defined(HITLS_CRYPTO_PROVIDER)
SetDecodePoolParamForKey(CRYPT_DECODER_PoolCtx * poolCtx,char * targetType,char * targetFormat)38 static int32_t SetDecodePoolParamForKey(CRYPT_DECODER_PoolCtx *poolCtx, char *targetType, char *targetFormat)
39 {
40     int32_t ret = CRYPT_DECODE_PoolCtrl(poolCtx, CRYPT_DECODE_POOL_CMD_SET_TARGET_FORMAT, targetFormat,
41         (int32_t)strlen(targetFormat));
42     if (ret != CRYPT_SUCCESS) {
43         return ret;
44     }
45     ret = CRYPT_DECODE_PoolCtrl(poolCtx, CRYPT_DECODE_POOL_CMD_SET_TARGET_TYPE, targetType,
46         (int32_t)strlen(targetType));
47     if (ret != CRYPT_SUCCESS) {
48         return ret;
49     }
50 
51     return ret;
52 }
53 
GetObjectFromOutData(BSL_Param * outData,void ** object)54 static int32_t GetObjectFromOutData(BSL_Param *outData, void **object)
55 {
56     if (outData == NULL || object == NULL) {
57         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
58         return CRYPT_NULL_INPUT;
59     }
60     BSL_Param *param = BSL_PARAM_FindParam(outData, CRYPT_PARAM_DECODE_OBJECT_DATA);
61     if (param == NULL || param->valueType != BSL_PARAM_TYPE_CTX_PTR || param->value == NULL) {
62         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
63         return CRYPT_INVALID_ARG;
64     }
65     *object = param->value;
66     return CRYPT_SUCCESS;
67 }
68 
CRYPT_EAL_ProviderDecodeBuffKeyInner(CRYPT_EAL_LibCtx * libCtx,const char * attrName,int32_t keyType,const char * format,const char * type,BSL_Buffer * encode,const BSL_Buffer * pwd,CRYPT_EAL_PkeyCtx ** ealPKey)69 int32_t CRYPT_EAL_ProviderDecodeBuffKeyInner(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType,
70     const char *format, const char *type, BSL_Buffer *encode, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey)
71 {
72     char *targetType = "HIGH_KEY";
73     char *targetFormat = "OBJECT";
74     uint32_t index = 0;
75     BSL_Param *outParam = NULL;
76     bool isFreeOutData = false;
77     BSL_Param input[3] = {{0}, {0}, BSL_PARAM_END};
78     CRYPT_EAL_PkeyCtx *tmpPKey = NULL;
79     if (encode == NULL || encode->data == NULL || encode->dataLen == 0 || ealPKey == NULL) {
80         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
81         return CRYPT_NULL_INPUT;
82     }
83     CRYPT_DECODER_PoolCtx *poolCtx = CRYPT_DECODE_PoolNewCtx(libCtx, attrName, keyType, format, type);
84     if (poolCtx == NULL) {
85         return CRYPT_MEM_ALLOC_FAIL;
86     }
87     int32_t ret = SetDecodePoolParamForKey(poolCtx, targetType, targetFormat);
88     if (ret != CRYPT_SUCCESS) {
89         goto EXIT;
90     }
91     (void)BSL_PARAM_InitValue(&input[index++], CRYPT_PARAM_DECODE_BUFFER_DATA, BSL_PARAM_TYPE_OCTETS, encode->data,
92         encode->dataLen);
93     if (pwd != NULL) {
94         (void)BSL_PARAM_InitValue(&input[index++], CRYPT_PARAM_DECODE_PASSWORD, BSL_PARAM_TYPE_OCTETS, pwd->data,
95             pwd->dataLen);
96     }
97     ret = CRYPT_DECODE_PoolDecode(poolCtx, input, &outParam);
98     if (ret != CRYPT_SUCCESS) {
99         goto EXIT;
100     }
101     ret = GetObjectFromOutData(outParam, (void **)(&tmpPKey));
102     if (ret != CRYPT_SUCCESS) {
103         goto EXIT;
104     }
105     int32_t algId = CRYPT_EAL_PkeyGetId(tmpPKey);
106     if (keyType != BSL_CID_UNKNOWN && algId != keyType) {
107         ret = CRYPT_EAL_ERR_ALGID;
108         BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID);
109         goto EXIT;
110     }
111     ret = CRYPT_DECODE_PoolCtrl(poolCtx, CRYPT_DECODE_POOL_CMD_SET_FLAG_FREE_OUT_DATA, &isFreeOutData, sizeof(bool));
112     if (ret != CRYPT_SUCCESS) {
113         goto EXIT;
114     }
115     *ealPKey = tmpPKey;
116     BSL_SAL_Free(outParam);
117 EXIT:
118     CRYPT_DECODE_PoolFreeCtx(poolCtx);
119     return ret;
120 }
121 
122 
123 #endif /* HITLS_CRYPTO_PROVIDER */
124 
CRYPT_EAL_ProviderDecodeBuffKey(CRYPT_EAL_LibCtx * libCtx,const char * attrName,int32_t keyType,const char * format,const char * type,BSL_Buffer * encode,const BSL_Buffer * pwd,CRYPT_EAL_PkeyCtx ** ealPKey)125 int32_t CRYPT_EAL_ProviderDecodeBuffKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType,
126     const char *format, const char *type, BSL_Buffer *encode, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey)
127 {
128 #ifdef HITLS_CRYPTO_PROVIDER
129     return CRYPT_EAL_ProviderDecodeBuffKeyInner(libCtx, attrName, keyType, format, type, encode, pwd, ealPKey);
130 #else
131     (void)libCtx;
132     (void)attrName;
133     (void)keyType;
134     int32_t encodeType = CRYPT_EAL_GetEncodeType(type);
135     int32_t encodeFormat = CRYPT_EAL_GetEncodeFormat(format);
136     if (pwd == NULL) {
137         return CRYPT_EAL_DecodeBuffKey(encodeFormat, encodeType, encode, NULL, 0, ealPKey);
138     } else {
139         return CRYPT_EAL_DecodeBuffKey(encodeFormat, encodeType, encode, pwd->data, pwd->dataLen, ealPKey);
140     }
141 #endif
142 }
143 
144 #ifdef HITLS_BSL_SAL_FILE
CRYPT_EAL_ProviderDecodeFileKey(CRYPT_EAL_LibCtx * libCtx,const char * attrName,int32_t keyType,const char * format,const char * type,const char * path,const BSL_Buffer * pwd,CRYPT_EAL_PkeyCtx ** ealPKey)145 int32_t CRYPT_EAL_ProviderDecodeFileKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType,
146     const char *format, const char *type, const char *path, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey)
147 {
148     if (path == NULL || strlen(path) > PATH_MAX_LEN) {
149         BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG);
150         return CRYPT_INVALID_ARG;
151     }
152 
153     uint8_t *data = NULL;
154     uint32_t dataLen = 0;
155     int32_t ret = BSL_SAL_ReadFile(path, &data, &dataLen);
156     if (ret != BSL_SUCCESS) {
157         BSL_ERR_PUSH_ERROR(ret);
158         return ret;
159     }
160     BSL_Buffer encode = {data, dataLen};
161     ret = CRYPT_EAL_ProviderDecodeBuffKey(libCtx, attrName, keyType, format, type, &encode, pwd, ealPKey);
162     BSL_SAL_Free(data);
163     return ret;
164 }
165 #endif /* HITLS_BSL_SAL_FILE */
166 
167 #endif /* HITLS_CRYPTO_CODECSKEY */
168