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 #ifdef HITLS_BSL_USRDATA
18
19 #include <stddef.h>
20 #include "bsl_errno.h"
21 #include "bsl_err_internal.h"
22 #include "bsl_user_data.h"
23
24 typedef struct {
25 long argl; /* Arbitrary long */
26 void *argp; /* Arbitrary void * */
27 BSL_USER_ExDataNew *newFunc;
28 BSL_USER_ExDataFree *freeFunc;
29 BSL_USER_ExDataDup *dupFunc;
30 } BSL_EX_CALLBACK;
31
32 BSL_EX_CALLBACK g_exCallBack[BSL_MAX_EX_TYPE][BSL_MAX_EX_DATA];
33
BSL_USER_GetExDataNewIndex(int32_t index,int64_t argl,const void * argp,const void * newFunc,const void * dupFunc,void * freeFunc)34 int BSL_USER_GetExDataNewIndex(int32_t index, int64_t argl, const void *argp, const void *newFunc, const void *dupFunc,
35 void *freeFunc)
36 {
37 if (index < 0 || index >= BSL_MAX_EX_TYPE) {
38 return -1;
39 }
40
41 (void)argl;
42 (void)argp;
43 (void)newFunc;
44 (void)dupFunc;
45 // The preceding parameters will not be used. Only the freefunc will be used.
46 static int idxSsl = 1; // The index starts from 1, 0 indicates app data.
47 static int idxX509StoreCtx = 1;
48 static int idxSslCtx = 1;
49 static int idxX509Store = 1;
50 static int idxUio = 1;
51 int idx = -1;
52 switch (index) {
53 case BSL_USER_DATA_EX_INDEX_SSL:
54 idx = idxSsl++;
55 break;
56 case BSL_USER_DATA_EX_INDEX_X509_STORE_CTX:
57 idx = idxX509StoreCtx++;
58 break;
59 case BSL_USER_DATA_EX_INDEX_SSL_CTX:
60 idx = idxSslCtx++;
61 break;
62 case BSL_USER_DATA_EX_INDEX_X509_STORE:
63 idx = idxX509Store++;
64 break;
65 case BSL_USER_DATA_EX_INDEX_UIO:
66 idx = idxUio++;
67 break;
68 default:
69 return -1;
70 }
71
72 if (idx != -1 && idx < BSL_MAX_EX_DATA) {
73 g_exCallBack[index][idx].freeFunc = freeFunc;
74 }
75
76 return idx;
77 }
78
BSL_USER_SetExData(BSL_USER_ExData * ad,int32_t idx,void * val)79 int BSL_USER_SetExData(BSL_USER_ExData *ad, int32_t idx, void *val)
80 {
81 if (ad == NULL || idx >= BSL_MAX_EX_DATA || idx < 0) {
82 BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT);
83 return BSL_NULL_INPUT;
84 }
85 ad->sk[idx] = val;
86 return BSL_SUCCESS;
87 }
88
BSL_USER_GetExData(const BSL_USER_ExData * ad,int32_t idx)89 void *BSL_USER_GetExData(const BSL_USER_ExData *ad, int32_t idx)
90 {
91 if (ad == NULL || idx >= BSL_MAX_EX_DATA || idx < 0) {
92 return NULL;
93 }
94 return ad->sk[idx];
95 }
96
BSL_USER_FreeExDataIndex(int32_t index,void * obj,BSL_USER_ExData * ad)97 void BSL_USER_FreeExDataIndex(int32_t index, void *obj, BSL_USER_ExData *ad)
98 {
99 if (index < 0 || index >= BSL_MAX_EX_TYPE || ad == NULL) {
100 return;
101 }
102
103 for (int32_t i = 0; i < BSL_MAX_EX_DATA; i++) {
104 if (ad->sk[i] != NULL && g_exCallBack[index][i].freeFunc != NULL) {
105 g_exCallBack[index][i].freeFunc(obj, ad->sk[i], ad, 0, 0, 0);
106 }
107 }
108 return;
109 }
110 #endif /* HITLS_BSL_USRDATA */
111