• 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 #ifdef HITLS_TLS_PROTO_DTLS12
18 #include "securec.h"
19 #include "bsl_module_list.h"
20 #include "tls_binlog_id.h"
21 #include "bsl_log_internal.h"
22 #include "bsl_log.h"
23 #include "bsl_err_internal.h"
24 #include "bsl_sal.h"
25 #include "hitls_error.h"
26 #include "rec.h"
27 #include "rec_unprocessed_msg.h"
28 
29 #ifdef HITLS_BSL_UIO_UDP
CacheNextEpochHsMsg(UnprocessedHsMsg * unprocessedHsMsg,const RecHdr * hdr,const uint8_t * recordBody)30 void CacheNextEpochHsMsg(UnprocessedHsMsg *unprocessedHsMsg, const RecHdr *hdr, const uint8_t *recordBody)
31 {
32     /* only out-of-order finished messages need to be cached */
33     if (hdr->type != REC_TYPE_HANDSHAKE) {
34         return;
35     }
36 
37     /* only cache one */
38     if (unprocessedHsMsg->recordBody != NULL) {
39         return;
40     }
41 
42     unprocessedHsMsg->recordBody = (uint8_t *)BSL_SAL_Dump(recordBody, hdr->bodyLen);
43     if (unprocessedHsMsg->recordBody == NULL) {
44         return;
45     }
46 
47     (void)memcpy_s(&unprocessedHsMsg->hdr, sizeof(RecHdr), hdr, sizeof(RecHdr));
48     BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15446, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN,
49         "cache next epoch hs msg", 0, 0, 0, 0);
50     return;
51 }
52 #endif /* HITLS_BSL_UIO_UDP */
53 
UnprocessedAppMsgNew(void)54 UnprocessedAppMsg *UnprocessedAppMsgNew(void)
55 {
56     UnprocessedAppMsg *msg = (UnprocessedAppMsg *)BSL_SAL_Calloc(1, sizeof(UnprocessedAppMsg));
57     if (msg == NULL) {
58         return NULL;
59     }
60 
61     LIST_INIT(&msg->head);
62     return msg;
63 }
64 
UnprocessedAppMsgFree(UnprocessedAppMsg * msg)65 void UnprocessedAppMsgFree(UnprocessedAppMsg *msg)
66 {
67     if (msg != NULL) {
68         BSL_SAL_FREE(msg->recordBody);
69         BSL_SAL_FREE(msg);
70     }
71     return;
72 }
73 
UnprocessedAppMsgListInit(UnprocessedAppMsg * appMsgList)74 void UnprocessedAppMsgListInit(UnprocessedAppMsg *appMsgList)
75 {
76     if (appMsgList == NULL) {
77         return;
78     }
79     appMsgList->count = 0;
80     appMsgList->recordBody = NULL;
81     LIST_INIT(&appMsgList->head);
82     return;
83 }
84 
UnprocessedAppMsgListDeinit(UnprocessedAppMsg * appMsgList)85 void UnprocessedAppMsgListDeinit(UnprocessedAppMsg *appMsgList)
86 {
87     ListHead *node = NULL;
88     ListHead *tmpNode = NULL;
89     UnprocessedAppMsg *cur = NULL;
90 
91     LIST_FOR_EACH_ITEM_SAFE(node, tmpNode, &(appMsgList->head)) {
92         cur = LIST_ENTRY(node, UnprocessedAppMsg, head);
93         LIST_REMOVE(node);
94         /* releasing nodes and deleting user data */
95         UnprocessedAppMsgFree(cur);
96     }
97     appMsgList->count = 0;
98     return;
99 }
100 
UnprocessedAppMsgListAppend(UnprocessedAppMsg * appMsgList,const RecHdr * hdr,const uint8_t * recordBody)101 int32_t UnprocessedAppMsgListAppend(UnprocessedAppMsg *appMsgList, const RecHdr *hdr, const uint8_t *recordBody)
102 {
103     /* prevent oversize */
104     if (appMsgList->count >= UNPROCESSED_APP_MSG_COUNT_MAX) {
105         return HITLS_REC_NORMAL_RECV_BUF_EMPTY;
106     }
107 
108     UnprocessedAppMsg *appNode = UnprocessedAppMsgNew();
109     if (appNode == NULL) {
110         BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
111         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15805, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
112             "Buffer app record: Malloc fail.", 0, 0, 0, 0);
113         return HITLS_MEMALLOC_FAIL;
114     }
115 
116     appNode->recordBody = (uint8_t*)BSL_SAL_Dump(recordBody, hdr->bodyLen);
117     if (appNode->recordBody == NULL) {
118         UnprocessedAppMsgFree(appNode);
119         BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
120         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15806, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
121             "Buffer app record: Malloc fail.", 0, 0, 0, 0);
122         return HITLS_MEMALLOC_FAIL;
123     }
124     (void)memcpy_s(&appNode->hdr, sizeof(RecHdr), hdr, sizeof(RecHdr));
125 
126     LIST_ADD_BEFORE(&appMsgList->head, &appNode->head);
127 
128     appMsgList->count++;
129     BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15807, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN,
130         "Buffer app record: count is %u.", appMsgList->count, 0, 0, 0);
131     return HITLS_SUCCESS;
132 }
133 
UnprocessedAppMsgGet(UnprocessedAppMsg * appMsgList,uint16_t curEpoch)134 UnprocessedAppMsg *UnprocessedAppMsgGet(UnprocessedAppMsg *appMsgList, uint16_t curEpoch)
135 {
136     ListHead *next = appMsgList->head.next;
137     if (next == &appMsgList->head) {
138         return NULL;
139     }
140 
141     ListHead *node = NULL;
142     ListHead *tmpNode = NULL;
143     UnprocessedAppMsg *cur = NULL;
144     LIST_FOR_EACH_ITEM_SAFE(node, tmpNode, &(appMsgList->head)) {
145         cur = LIST_ENTRY(node, UnprocessedAppMsg, head);
146         uint16_t epoch = REC_EPOCH_GET(cur->hdr.epochSeq);
147         if (curEpoch == epoch) {
148             /* remove a node and release it by the outside */
149             LIST_REMOVE(node);
150             appMsgList->count--;
151             return cur;
152         }
153     }
154     return NULL;
155 }
156 
157 #endif /* HITLS_TLS_PROTO_DTLS12 */
158