• 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 "securec.h"
17 #include "bsl_bytes.h"
18 #include "bsl_sal.h"
19 #include "hitls_error.h"
20 #include "hitls_crypt_type.h"
21 #include "tls.h"
22 #include "hs_ctx.h"
23 #include "hs_extensions.h"
24 #include "frame_tls.h"
25 #include "frame_msg.h"
26 
27 #define SIZE_OF_UINT_24 3u
28 #define SIZE_OF_UINT_48 6u
29 
ParseFieldInteger8(const uint8_t * buffer,uint32_t bufLen,FRAME_Integer * field,uint32_t * offset)30 static int32_t ParseFieldInteger8(const uint8_t *buffer, uint32_t bufLen, FRAME_Integer *field, uint32_t *offset)
31 {
32     if (bufLen < sizeof(uint8_t)) {
33         return HITLS_PARSE_INVALID_MSG_LEN;
34     }
35     field->state = INITIAL_FIELD;
36     field->data = buffer[0];
37     *offset += sizeof(uint8_t);
38     return HITLS_SUCCESS;
39 }
40 
ParseFieldInteger16(const uint8_t * buffer,uint32_t bufLen,FRAME_Integer * field,uint32_t * offset)41 static int32_t ParseFieldInteger16(const uint8_t *buffer, uint32_t bufLen, FRAME_Integer *field, uint32_t *offset)
42 {
43     if (bufLen < sizeof(uint16_t)) {
44         return HITLS_PARSE_INVALID_MSG_LEN;
45     }
46     field->state = INITIAL_FIELD;
47     field->data = BSL_ByteToUint16(buffer);
48     *offset += sizeof(uint16_t);
49     return HITLS_SUCCESS;
50 }
51 
ParseFieldInteger24(const uint8_t * buffer,uint32_t bufLen,FRAME_Integer * field,uint32_t * offset)52 static int32_t ParseFieldInteger24(const uint8_t *buffer, uint32_t bufLen, FRAME_Integer *field, uint32_t *offset)
53 {
54     if (bufLen < SIZE_OF_UINT_24) {
55         return HITLS_PARSE_INVALID_MSG_LEN;
56     }
57     field->state = INITIAL_FIELD;
58     field->data = BSL_ByteToUint24(buffer);
59     *offset += SIZE_OF_UINT_24;
60     return HITLS_SUCCESS;
61 }
62 
ParseFieldInteger32(const uint8_t * buffer,uint32_t bufLen,FRAME_Integer * field,uint32_t * offset)63 static int32_t ParseFieldInteger32(const uint8_t *buffer, uint32_t bufLen, FRAME_Integer *field, uint32_t *offset)
64 {
65     if (bufLen < sizeof(uint32_t)) {
66         return HITLS_PARSE_INVALID_MSG_LEN;
67     }
68     field->state = INITIAL_FIELD;
69     field->data = BSL_ByteToUint32(buffer);
70     *offset += sizeof(uint32_t);
71     return HITLS_SUCCESS;
72 }
73 
ParseFieldInteger48(const uint8_t * buffer,uint32_t bufLen,FRAME_Integer * field,uint32_t * offset)74 static int32_t ParseFieldInteger48(const uint8_t *buffer, uint32_t bufLen, FRAME_Integer *field, uint32_t *offset)
75 {
76     if (bufLen < SIZE_OF_UINT_48) {
77         return HITLS_PARSE_INVALID_MSG_LEN;
78     }
79     field->state = INITIAL_FIELD;
80     field->data = BSL_ByteToUint48(buffer);
81     *offset += SIZE_OF_UINT_48;
82     return HITLS_SUCCESS;
83 }
84 
ParseFieldArray8(const uint8_t * buffer,uint32_t bufLen,FRAME_Array8 * field,uint32_t fieldLen,uint32_t * offset)85 static int32_t ParseFieldArray8(const uint8_t *buffer, uint32_t bufLen, FRAME_Array8 *field, uint32_t fieldLen,
86                                 uint32_t *offset)
87 {
88     if (bufLen < fieldLen) {
89         return HITLS_PARSE_INVALID_MSG_LEN;
90     }
91     BSL_SAL_FREE(field->data);
92     field->data = BSL_SAL_Dump(buffer, fieldLen);
93     if (field->data == NULL) {
94         return HITLS_MEMALLOC_FAIL;
95     }
96     field->size = fieldLen;
97     field->state = INITIAL_FIELD;
98     *offset += fieldLen;
99     return HITLS_SUCCESS;
100 }
101 
ParseFieldArray16(const uint8_t * buffer,uint32_t bufLen,FRAME_Array16 * field,uint32_t fieldLen,uint32_t * offset)102 static int32_t ParseFieldArray16(const uint8_t *buffer, uint32_t bufLen, FRAME_Array16 *field, uint32_t fieldLen,
103                                  uint32_t *offset)
104 {
105     if ((bufLen < fieldLen) || (fieldLen % sizeof(uint16_t) != 0)) {
106         return HITLS_PARSE_INVALID_MSG_LEN;
107     }
108     field->data = BSL_SAL_Calloc(1u, fieldLen);
109     if (field->data == NULL) {
110         return HITLS_MEMALLOC_FAIL;
111     }
112     field->size = fieldLen / sizeof(uint16_t);
113     for (uint32_t i = 0; i < field->size; i++) {
114         field->data[i] = BSL_ByteToUint16(&buffer[i * sizeof(uint16_t)]);
115     }
116     field->state = INITIAL_FIELD;
117     *offset += fieldLen;
118     return HITLS_SUCCESS;
119 }
120 
ParseHsExtArray8(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtArray8 * field,uint32_t * offset)121 static int32_t ParseHsExtArray8(const uint8_t *buffer, uint32_t bufLen, FRAME_HsExtArray8 *field, uint32_t *offset)
122 {
123     uint32_t exOffset = 0;
124     field->exState = INITIAL_FIELD;
125     ParseFieldInteger16(&buffer[0], bufLen, &field->exType, &exOffset);
126     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
127     if (field->exLen.data == 0u) {
128         *offset += exOffset;
129         return HITLS_SUCCESS;
130     }
131     ParseFieldInteger8(&buffer[exOffset], bufLen - exOffset, &field->exDataLen, &exOffset);
132     ParseFieldArray8(&buffer[exOffset], bufLen - exOffset, &field->exData, field->exDataLen.data, &exOffset);
133     *offset += exOffset;
134     return HITLS_SUCCESS;
135 }
136 
ParseHsExtArrayForList(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtArray8 * field,uint32_t * offset)137 static int32_t ParseHsExtArrayForList(
138     const uint8_t *buffer, uint32_t bufLen, FRAME_HsExtArray8 *field, uint32_t *offset)
139 {
140     uint32_t exOffset = 0;
141     field->exState = INITIAL_FIELD;
142     ParseFieldInteger16(&buffer[0], bufLen, &field->exType, &exOffset);
143     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
144     if (field->exLen.data == 0u) {
145         *offset += exOffset;
146         return HITLS_SUCCESS;
147     }
148     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exDataLen, &exOffset);
149     ParseFieldArray8(&buffer[exOffset], bufLen - exOffset, &field->exData, field->exDataLen.data, &exOffset);
150     *offset += exOffset;
151     return HITLS_SUCCESS;
152 }
153 
ParseHsSessionTicketExtArray8(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtArray8 * field,uint32_t * offset)154 static int32_t ParseHsSessionTicketExtArray8(
155     const uint8_t *buffer, uint32_t bufLen, FRAME_HsExtArray8 *field, uint32_t *offset)
156 {
157     uint32_t exOffset = 0;
158     field->exState = INITIAL_FIELD;
159     ParseFieldInteger16(&buffer[0], bufLen, &field->exType, &exOffset);
160     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exDataLen, &exOffset);
161     if (field->exDataLen.data == 0u) {
162         *offset += exOffset;
163         return HITLS_SUCCESS;
164     }
165     ParseFieldArray8(&buffer[exOffset], bufLen - exOffset, &field->exData, field->exDataLen.data, &exOffset);
166     *offset += exOffset;
167     return HITLS_SUCCESS;
168 }
169 
ParseHsExtArray16(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtArray16 * field,uint32_t * offset)170 static int32_t ParseHsExtArray16(const uint8_t *buffer, uint32_t bufLen, FRAME_HsExtArray16 *field, uint32_t *offset)
171 {
172     uint32_t exOffset = 0;
173     field->exState = INITIAL_FIELD;
174     ParseFieldInteger16(&buffer[0], bufLen - exOffset, &field->exType, &exOffset);
175     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
176     if (field->exLen.data == 0u) {
177         *offset += exOffset;
178         return HITLS_SUCCESS;
179     }
180     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exDataLen, &exOffset);
181     ParseFieldArray16(&buffer[exOffset], bufLen - exOffset, &field->exData, field->exDataLen.data, &exOffset);
182     *offset += exOffset;
183     return HITLS_SUCCESS;
184 }
185 
ParseHsExtPskIdentity(const uint8_t * buffer,uint32_t bufLen,FRAME_HsArrayPskIdentity * field,uint32_t fieldLen,uint32_t * offset)186 static int32_t ParseHsExtPskIdentity(const uint8_t *buffer, uint32_t bufLen, FRAME_HsArrayPskIdentity *field,
187     uint32_t fieldLen, uint32_t *offset)
188 {
189     uint32_t exOffset = 0;
190     field->state = INITIAL_FIELD;
191     uint32_t size = 0;
192     FRAME_Integer tmpIdentityLen = { 0 };
193     while (exOffset < fieldLen) {
194         ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &tmpIdentityLen, &exOffset);
195         exOffset += (tmpIdentityLen.data + sizeof(uint32_t));
196         if (exOffset <= fieldLen) {
197             size++;
198         }
199     }
200     if (size == 0) {
201         return HITLS_SUCCESS;
202     }
203     field->data = BSL_SAL_Calloc(size, sizeof(FRAME_HsPskIdentity));
204     if (field->data == NULL) {
205         return HITLS_MEMALLOC_FAIL;
206     }
207     field->size = size;
208     exOffset = 0;
209     for (uint32_t i = 0; i < size; i++) {
210         field->data[i].state = INITIAL_FIELD;
211         ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->data[i].identityLen, &exOffset);
212         ParseFieldArray8(&buffer[exOffset], bufLen - exOffset, &field->data[i].identity,
213             field->data[i].identityLen.data, &exOffset);
214         ParseFieldInteger32(&buffer[exOffset], bufLen - exOffset, &field->data[i].obfuscatedTicketAge, &exOffset);
215     }
216     *offset += exOffset;
217     return HITLS_SUCCESS;
218 }
219 
ParseHsExtPskBinder(const uint8_t * buffer,uint32_t bufLen,FRAME_HsArrayPskBinder * field,uint32_t fieldLen,uint32_t * offset)220 static int32_t ParseHsExtPskBinder(const uint8_t *buffer, uint32_t bufLen, FRAME_HsArrayPskBinder *field,
221     uint32_t fieldLen, uint32_t *offset)
222 {
223     uint32_t exOffset = 0;
224     field->state = INITIAL_FIELD;
225     uint32_t size = 0;
226     FRAME_Integer tmpBinderLen = { 0 };
227     while (exOffset < fieldLen) {
228         ParseFieldInteger8(&buffer[exOffset], bufLen - exOffset, &tmpBinderLen, &exOffset);
229         exOffset += tmpBinderLen.data;
230         if (exOffset <= fieldLen) {
231             size++;
232         }
233     }
234     if (size == 0) {
235         return HITLS_SUCCESS;
236     }
237     field->data = BSL_SAL_Calloc(size, sizeof(FRAME_HsPskBinder));
238     if (field->data == NULL) {
239         return HITLS_MEMALLOC_FAIL;
240     }
241     field->size = size;
242     exOffset = 0;
243     for (uint32_t i = 0; i < size; i++) {
244         field->data[i].state = INITIAL_FIELD;
245         ParseFieldInteger8(&buffer[exOffset], bufLen - exOffset, &field->data[i].binderLen, &exOffset);
246         ParseFieldArray8(&buffer[exOffset], bufLen - exOffset, &field->data[i].binder,
247             field->data[i].binderLen.data, &exOffset);
248     }
249     *offset += exOffset;
250     return HITLS_SUCCESS;
251 }
252 
ParseHsExtPsk(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtOfferedPsks * field,uint32_t * offset)253 static int32_t ParseHsExtPsk(const uint8_t *buffer, uint32_t bufLen, FRAME_HsExtOfferedPsks *field, uint32_t *offset)
254 {
255     uint32_t exOffset = 0;
256     field->exState = INITIAL_FIELD;
257     ParseFieldInteger16(&buffer[0], bufLen - exOffset, &field->exType, &exOffset);
258     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
259     if (field->exLen.data == 0u) {
260         *offset += exOffset;
261         return HITLS_SUCCESS;
262     }
263     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->identitySize, &exOffset);
264     ParseHsExtPskIdentity(&buffer[exOffset], bufLen - exOffset, &field->identities,
265         field->identitySize.data, &exOffset);
266     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->binderSize, &exOffset);
267     ParseHsExtPskBinder(&buffer[exOffset], bufLen - exOffset, &field->binders, field->binderSize.data, &exOffset);
268     *offset += exOffset;
269     return HITLS_SUCCESS;
270 }
271 
ParseHsExtArrayKeyShare(const uint8_t * buffer,uint32_t bufLen,FRAME_HsArrayKeyShare * field,uint32_t fieldLen,uint32_t * offset)272 static int32_t ParseHsExtArrayKeyShare(const uint8_t *buffer, uint32_t bufLen, FRAME_HsArrayKeyShare *field,
273     uint32_t fieldLen, uint32_t *offset)
274 {
275     uint32_t exOffset = 0;
276     field->state = INITIAL_FIELD;
277     uint32_t size = 0;
278     FRAME_Integer tmpIdentityLen = { 0 };
279     while (exOffset < fieldLen) {
280         ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &tmpIdentityLen, &exOffset); // group
281         ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &tmpIdentityLen, &exOffset); // key_exchange len
282         exOffset += tmpIdentityLen.data;
283         if (exOffset <= fieldLen) {
284             size++;
285         }
286     }
287     if (size == 0) {
288         return HITLS_SUCCESS;
289     }
290     field->data = BSL_SAL_Calloc(size, sizeof(FRAME_HsKeyShareEntry));
291     if (field->data == NULL) {
292         return HITLS_MEMALLOC_FAIL;
293     }
294     field->size = size;
295     exOffset = 0;
296     for (uint32_t i = 0; i < size; i++) {
297         field->data[i].state = INITIAL_FIELD;
298         ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->data[i].group, &exOffset);
299         ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->data[i].keyExchangeLen, &exOffset);
300         ParseFieldArray8(&buffer[exOffset], bufLen - exOffset, &field->data[i].keyExchange,
301             field->data[i].keyExchangeLen.data, &exOffset);
302     }
303     *offset += exOffset;
304     return HITLS_SUCCESS;
305 }
306 
ParseHsExtKeyShare(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtKeyShare * field,uint32_t * offset)307 static int32_t ParseHsExtKeyShare(const uint8_t *buffer, uint32_t bufLen, FRAME_HsExtKeyShare *field, uint32_t *offset)
308 {
309     uint32_t exOffset = 0;
310     field->exState = INITIAL_FIELD;
311     ParseFieldInteger16(&buffer[0], bufLen - exOffset, &field->exType, &exOffset);
312     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
313     if (field->exLen.data == 0u) {
314         *offset += exOffset;
315         return HITLS_SUCCESS;
316     }
317     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exKeyShareLen, &exOffset);
318     ParseHsExtArrayKeyShare(&buffer[exOffset], bufLen - exOffset, &field->exKeyShares,
319         field->exKeyShareLen.data, &exOffset);
320     *offset += exOffset;
321     return HITLS_SUCCESS;
322 }
323 
ParseHsSupportedVersion(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtArray16 * field,uint32_t * offset)324 static int32_t ParseHsSupportedVersion(const uint8_t *buffer, uint32_t bufLen,
325     FRAME_HsExtArray16 *field, uint32_t *offset)
326 {
327     uint32_t exOffset = 0;
328     field->exState = INITIAL_FIELD;
329     ParseFieldInteger16(&buffer[0], bufLen - exOffset, &field->exType, &exOffset);
330     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
331     if (field->exLen.data == 0u) {
332         *offset += exOffset;
333         return HITLS_SUCCESS;
334     }
335     ParseFieldInteger8(&buffer[exOffset], bufLen - exOffset, &field->exDataLen, &exOffset);
336     ParseFieldArray16(&buffer[exOffset], bufLen - exOffset, &field->exData, field->exDataLen.data, &exOffset);
337     *offset += exOffset;
338     return HITLS_SUCCESS;
339 }
340 
ParseClientHelloMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_ClientHelloMsg * clientHello,uint32_t * parseLen)341 static int32_t ParseClientHelloMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
342     FRAME_ClientHelloMsg *clientHello, uint32_t *parseLen)
343 {
344     uint32_t offset = 0;
345     ParseFieldInteger16(&buffer[0], bufLen, &clientHello->version, &offset);
346     ParseFieldArray8(&buffer[offset], bufLen - offset, &clientHello->randomValue, HS_RANDOM_SIZE, &offset);
347     ParseFieldInteger8(&buffer[offset], bufLen - offset, &clientHello->sessionIdSize, &offset);
348     ParseFieldArray8(&buffer[offset], bufLen - offset, &clientHello->sessionId,
349                      clientHello->sessionIdSize.data, &offset);
350     if (IS_TRANSTYPE_DATAGRAM(frameType->transportType)) {
351         ParseFieldInteger8(&buffer[offset], bufLen - offset, &clientHello->cookiedLen, &offset);
352         ParseFieldArray8(&buffer[offset], bufLen - offset, &clientHello->cookie, clientHello->cookiedLen.data, &offset);
353     }
354     ParseFieldInteger16(&buffer[offset], bufLen - offset, &clientHello->cipherSuitesSize, &offset);
355     ParseFieldArray16(&buffer[offset], bufLen - offset, &clientHello->cipherSuites,
356                       clientHello->cipherSuitesSize.data, &offset);
357     ParseFieldInteger8(&buffer[offset], bufLen - offset, &clientHello->compressionMethodsLen, &offset);
358     ParseFieldArray8(&buffer[offset], bufLen - offset, &clientHello->compressionMethods,
359                      clientHello->compressionMethodsLen.data, &offset);
360     ParseFieldInteger16(&buffer[offset], bufLen - offset, &clientHello->extensionLen, &offset);
361     clientHello->extensionState = INITIAL_FIELD;
362 
363     /* Parsing extended fields */
364     while (offset < bufLen) {
365         FRAME_Integer tmpField = {0};
366         uint32_t tmpOffset = offset;
367         ParseFieldInteger16(&buffer[tmpOffset], bufLen - tmpOffset, &tmpField, &tmpOffset);
368         switch (tmpField.data) {
369             case HS_EX_TYPE_POINT_FORMATS:
370                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &clientHello->pointFormats, &offset);
371                 break;
372             case HS_EX_TYPE_SUPPORTED_GROUPS:
373                 ParseHsExtArray16(&buffer[offset], bufLen - offset, &clientHello->supportedGroups, &offset);
374                 break;
375             case HS_EX_TYPE_SIGNATURE_ALGORITHMS:
376                 ParseHsExtArray16(&buffer[offset], bufLen - offset, &clientHello->signatureAlgorithms, &offset);
377                 break;
378             case HS_EX_TYPE_EXTENDED_MASTER_SECRET:
379                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &clientHello->extendedMasterSecret, &offset);
380                 break;
381             case HS_EX_TYPE_RENEGOTIATION_INFO:
382                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &clientHello->secRenego, &offset);
383                 break;
384             case HS_EX_TYPE_SESSION_TICKET:
385                 ParseHsSessionTicketExtArray8(&buffer[offset], bufLen - offset, &clientHello->sessionTicket, &offset);
386                 break;
387             case HS_EX_TYPE_SERVER_NAME:
388                 ParseHsExtArrayForList(&buffer[offset], bufLen - offset, &clientHello->serverName, &offset);
389                 break;
390             case HS_EX_TYPE_APP_LAYER_PROTOCOLS:
391                 ParseHsExtArrayForList(&buffer[offset], bufLen - offset, &clientHello->alpn, &offset);
392                 break;
393             case HS_EX_TYPE_KEY_SHARE:
394                 ParseHsExtKeyShare(&buffer[offset], bufLen - offset, &clientHello->keyshares, &offset);
395                 break;
396             case HS_EX_TYPE_PRE_SHARED_KEY:
397                 ParseHsExtPsk(&buffer[offset], bufLen - offset, &clientHello->psks, &offset);
398                 break;
399             case HS_EX_TYPE_PSK_KEY_EXCHANGE_MODES:
400                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &clientHello->pskModes, &offset);
401                 break;
402             case HS_EX_TYPE_SUPPORTED_VERSIONS:
403                 ParseHsSupportedVersion(&buffer[offset], bufLen - offset, &clientHello->supportedVersion, &offset);
404                 break;
405             case HS_EX_TYPE_COOKIE:
406                 ParseHsExtArrayForList(&buffer[offset], bufLen - offset, &clientHello->tls13Cookie, &offset);
407                 break;
408             case HS_EX_TYPE_ENCRYPT_THEN_MAC:
409                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &clientHello->encryptThenMac, &offset);
410                 break;
411             default: /* Unrecognized extension. Skip parsing the extension. */
412                 ParseFieldInteger16(&buffer[tmpOffset], bufLen - tmpOffset, &tmpField, &tmpOffset);
413                 tmpOffset += tmpField.data;
414                 offset = tmpOffset;
415                 break;
416         }
417         if (tmpOffset == offset) {
418             break;
419         }
420     }
421     *parseLen += offset;
422     return HITLS_SUCCESS;
423 }
424 
CleanClientHelloMsg(FRAME_ClientHelloMsg * clientHello)425 static void CleanClientHelloMsg(FRAME_ClientHelloMsg *clientHello)
426 {
427     BSL_SAL_FREE(clientHello->randomValue.data);
428     BSL_SAL_FREE(clientHello->sessionId.data);
429     BSL_SAL_FREE(clientHello->cookie.data);
430     BSL_SAL_FREE(clientHello->cipherSuites.data);
431     BSL_SAL_FREE(clientHello->compressionMethods.data);
432     BSL_SAL_FREE(clientHello->pointFormats.exData.data);
433     BSL_SAL_FREE(clientHello->supportedGroups.exData.data);
434     BSL_SAL_FREE(clientHello->signatureAlgorithms.exData.data);
435     BSL_SAL_FREE(clientHello->extendedMasterSecret.exData.data);
436     BSL_SAL_FREE(clientHello->secRenego.exData.data);
437     BSL_SAL_FREE(clientHello->sessionTicket.exData.data);
438     BSL_SAL_FREE(clientHello->serverName.exData.data);
439     BSL_SAL_FREE(clientHello->alpn.exData.data);
440     for (uint32_t i = 0; i < clientHello->keyshares.exKeyShares.size; i++) {
441         BSL_SAL_FREE(clientHello->keyshares.exKeyShares.data[i].keyExchange.data);
442     }
443     for (uint32_t i = 0; i < clientHello->psks.identities.size; i++) {
444         BSL_SAL_FREE(clientHello->psks.identities.data[i].identity.data);
445     }
446     for (uint32_t i = 0; i < clientHello->psks.binders.size; i++) {
447         BSL_SAL_FREE(clientHello->psks.binders.data[i].binder.data);
448     }
449     BSL_SAL_FREE(clientHello->keyshares.exKeyShares.data);
450     BSL_SAL_FREE(clientHello->psks.binders.data);
451     BSL_SAL_FREE(clientHello->psks.identities.data);
452     BSL_SAL_FREE(clientHello->supportedVersion.exData.data);
453     BSL_SAL_FREE(clientHello->tls13Cookie.exData.data);
454     BSL_SAL_FREE(clientHello->pskModes.exData.data);
455     BSL_SAL_FREE(clientHello->caList.list.data);
456     return;
457 }
458 
ParseHsExtUint16(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtUint16 * field,uint32_t * offset)459 static int32_t ParseHsExtUint16(const uint8_t *buffer, uint32_t bufLen, FRAME_HsExtUint16 *field, uint32_t *offset)
460 {
461     uint32_t exOffset = 0;
462     field->exState = INITIAL_FIELD;
463     ParseFieldInteger16(&buffer[0], bufLen, &field->exType, &exOffset);
464     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
465     if (field->exLen.data == 0u) {
466         *offset += exOffset;
467         return HITLS_SUCCESS;
468     }
469     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->data, &exOffset);
470     *offset += exOffset;
471     return HITLS_SUCCESS;
472 }
473 
ParseHsExtServerKeyShare(const uint8_t * buffer,uint32_t bufLen,FRAME_HsExtServerKeyShare * field,uint32_t * offset)474 static int32_t ParseHsExtServerKeyShare(const uint8_t *buffer, uint32_t bufLen,
475     FRAME_HsExtServerKeyShare *field, uint32_t *offset)
476 {
477     uint32_t exOffset = 0;
478     field->exState = INITIAL_FIELD;
479     ParseFieldInteger16(&buffer[0], bufLen, &field->exType, &exOffset);
480     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->exLen, &exOffset);
481     if (field->exLen.data == 0u) {
482         *offset += exOffset;
483         return HITLS_SUCCESS;
484     }
485     field->data.state = INITIAL_FIELD;
486     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->data.group, &exOffset);
487     ParseFieldInteger16(&buffer[exOffset], bufLen - exOffset, &field->data.keyExchangeLen, &exOffset);
488     ParseFieldArray8(&buffer[exOffset], bufLen - exOffset, &field->data.keyExchange,
489         field->data.keyExchangeLen.data, &exOffset);
490     *offset += exOffset;
491     return HITLS_SUCCESS;
492 }
493 
ParseServerHelloMsg(const uint8_t * buffer,uint32_t bufLen,FRAME_ServerHelloMsg * serverHello,uint32_t * parseLen)494 static int32_t ParseServerHelloMsg(const uint8_t *buffer, uint32_t bufLen, FRAME_ServerHelloMsg *serverHello,
495                                    uint32_t *parseLen)
496 {
497     uint32_t offset = 0;
498     ParseFieldInteger16(&buffer[0], bufLen, &serverHello->version, &offset);
499     ParseFieldArray8(&buffer[offset], bufLen - offset, &serverHello->randomValue, HS_RANDOM_SIZE, &offset);
500     ParseFieldInteger8(&buffer[offset], bufLen - offset, &serverHello->sessionIdSize, &offset);
501     ParseFieldArray8(&buffer[offset], bufLen - offset, &serverHello->sessionId,
502                      serverHello->sessionIdSize.data, &offset);
503     ParseFieldInteger16(&buffer[offset], bufLen - offset, &serverHello->cipherSuite, &offset);
504     ParseFieldInteger8(&buffer[offset], bufLen - offset, &serverHello->compressionMethod, &offset);
505     ParseFieldInteger16(&buffer[offset], bufLen - offset, &serverHello->extensionLen, &offset);
506 
507     /* Parsing extended fields */
508     while (offset < bufLen) {
509         FRAME_Integer tmpField = {0};
510         uint32_t tmpOffset = offset;
511         ParseFieldInteger16(&buffer[tmpOffset], bufLen - tmpOffset, &tmpField, &tmpOffset);
512         switch (tmpField.data) {
513             case HS_EX_TYPE_POINT_FORMATS:
514                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &serverHello->pointFormats, &offset);
515                 break;
516             case HS_EX_TYPE_EXTENDED_MASTER_SECRET:
517                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &serverHello->extendedMasterSecret, &offset);
518                 break;
519             case HS_EX_TYPE_RENEGOTIATION_INFO:
520                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &serverHello->secRenego, &offset);
521                 break;
522             case HS_EX_TYPE_SESSION_TICKET:
523                 ParseHsSessionTicketExtArray8(&buffer[offset], bufLen - offset, &serverHello->sessionTicket, &offset);
524                 break;
525             case HS_EX_TYPE_SERVER_NAME:
526                 ParseHsExtArrayForList(&buffer[offset], bufLen - offset, &serverHello->serverName, &offset);
527                 break;
528             case HS_EX_TYPE_APP_LAYER_PROTOCOLS:
529                 ParseHsExtArrayForList(&buffer[offset], bufLen - offset, &serverHello->alpn, &offset);
530                 break;
531             case HS_EX_TYPE_SUPPORTED_VERSIONS:
532                 ParseHsExtUint16(&buffer[offset], bufLen - offset, &serverHello->supportedVersion, &offset);
533                 break;
534             case HS_EX_TYPE_KEY_SHARE:
535                 ParseHsExtServerKeyShare(&buffer[offset], bufLen - offset, &serverHello->keyShare, &offset);
536                 break;
537             case HS_EX_TYPE_PRE_SHARED_KEY:
538                 ParseHsExtUint16(&buffer[offset], bufLen - offset, &serverHello->pskSelectedIdentity, &offset);
539                 break;
540             case HS_EX_TYPE_COOKIE:
541                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &serverHello->tls13Cookie, &offset);
542                 break;
543             case HS_EX_TYPE_ENCRYPT_THEN_MAC:
544                 ParseHsExtArray8(&buffer[offset], bufLen - offset, &serverHello->encryptThenMac, &offset);
545                 break;
546             default: /* Unrecognized extension, return error */
547                 *parseLen += offset;
548                 return HITLS_PARSE_UNSUPPORTED_EXTENSION;
549         }
550     }
551     *parseLen += offset;
552     return HITLS_SUCCESS;
553 }
554 
CleanServerHelloMsg(FRAME_ServerHelloMsg * serverHello)555 static void CleanServerHelloMsg(FRAME_ServerHelloMsg *serverHello)
556 {
557     BSL_SAL_FREE(serverHello->randomValue.data);
558     BSL_SAL_FREE(serverHello->sessionId.data);
559     BSL_SAL_FREE(serverHello->pointFormats.exData.data);
560     BSL_SAL_FREE(serverHello->extendedMasterSecret.exData.data);
561     BSL_SAL_FREE(serverHello->secRenego.exData.data);
562     BSL_SAL_FREE(serverHello->sessionTicket.exData.data);
563     BSL_SAL_FREE(serverHello->serverName.exData.data);
564     BSL_SAL_FREE(serverHello->alpn.exData.data);
565     BSL_SAL_FREE(serverHello->keyShare.data.keyExchange.data);
566     BSL_SAL_FREE(serverHello->tls13Cookie.exData.data);
567     return;
568 }
569 
ParseCertificateMsg(FRAME_Type * type,const uint8_t * buffer,uint32_t bufLen,FRAME_CertificateMsg * certificate,uint32_t * parseLen)570 static int32_t ParseCertificateMsg(
571     FRAME_Type *type, const uint8_t *buffer, uint32_t bufLen, FRAME_CertificateMsg *certificate, uint32_t *parseLen)
572 {
573     uint32_t offset = 0;
574     if (type->versionType == HITLS_VERSION_TLS13) {
575         ParseFieldInteger8(&buffer[0], bufLen, &certificate->certificateReqCtxSize, &offset);
576         ParseFieldArray8(&buffer[offset], bufLen - offset, &certificate->certificateReqCtx,
577                             certificate->certificateReqCtxSize.data, &offset);
578     }
579     ParseFieldInteger24(&buffer[offset], bufLen - offset, &certificate->certsLen, &offset);
580     if (certificate->certsLen.data == 0) {
581         *parseLen += offset;
582         return HITLS_SUCCESS;
583     }
584 
585     FrameCertItem *certItem = NULL;
586     while (offset < bufLen) {
587         uint32_t tmpOffset = offset;
588         FrameCertItem *item = BSL_SAL_Calloc(1u, sizeof(FrameCertItem));
589         if (item == NULL) {
590             return HITLS_MEMALLOC_FAIL;
591         }
592         item->state = INITIAL_FIELD;
593         ParseFieldInteger24(&buffer[offset], bufLen - offset, &item->certLen, &offset);
594         ParseFieldArray8(&buffer[offset], bufLen - offset, &item->cert, item->certLen.data, &offset);
595         if (type->versionType == HITLS_VERSION_TLS13) {
596             ParseFieldInteger16(&buffer[offset], bufLen - offset, &item->extensionLen, &offset);
597             ParseFieldArray8(&buffer[offset], bufLen - offset, &item->extension, item->extensionLen.data, &offset);
598         }
599         if (certificate->certItem == NULL) {
600             certificate->certItem = item;
601         } else {
602             certItem->next = item;
603         }
604         certItem = item;
605         if (tmpOffset == offset) {
606             break;
607         }
608     }
609     *parseLen += offset;
610 
611     return HITLS_SUCCESS;
612 }
613 
CleanCertificateMsg(FRAME_CertificateMsg * certificate)614 static void CleanCertificateMsg(FRAME_CertificateMsg *certificate)
615 {
616     BSL_SAL_FREE(certificate->certificateReqCtx.data);
617     FrameCertItem *certItem = certificate->certItem;
618     while (certItem != NULL) {
619         FrameCertItem *temp = certItem->next;
620         BSL_SAL_FREE(certItem->cert.data);
621         BSL_SAL_FREE(certItem->extension.data);
622         BSL_SAL_FREE(certItem);
623         certItem = temp;
624     }
625     certificate->certItem = NULL;
626     return;
627 }
628 
ParseServerKxEcdhMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_ServerEcdh * ecdh,uint32_t * parseLen)629 static int32_t ParseServerKxEcdhMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
630                                     FRAME_ServerEcdh *ecdh, uint32_t *parseLen)
631 {
632     uint32_t offset = 0;
633     ParseFieldInteger8(&buffer[0], bufLen, &ecdh->curveType, &offset);
634     if (ecdh->curveType.data != HITLS_EC_CURVE_TYPE_NAMED_CURVE) {
635         return HITLS_PARSE_UNSUPPORT_KX_ALG;
636     }
637     ParseFieldInteger16(&buffer[offset], bufLen - offset, &ecdh->namedcurve, &offset);
638     ParseFieldInteger8(&buffer[offset], bufLen - offset, &ecdh->pubKeySize, &offset);
639     ParseFieldArray8(&buffer[offset], bufLen - offset, &ecdh->pubKey, ecdh->pubKeySize.data, &offset);
640     if (((!IS_DTLS_VERSION(frameType->versionType)) && (frameType->versionType >= HITLS_VERSION_TLS12)) ||
641         ((IS_DTLS_VERSION(frameType->versionType)) && (frameType->versionType <= HITLS_VERSION_DTLS12))) {
642         ParseFieldInteger16(&buffer[offset], bufLen - offset, &ecdh->signAlgorithm, &offset);
643     }
644     ParseFieldInteger16(&buffer[offset], bufLen - offset, &ecdh->signSize, &offset);
645     ParseFieldArray8(&buffer[offset], bufLen - offset, &ecdh->signData, ecdh->signSize.data, &offset);
646     *parseLen += offset;
647     return HITLS_SUCCESS;
648 }
649 
ParseServerKxDhMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_ServerDh * dh,uint32_t * parseLen)650 static int32_t ParseServerKxDhMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
651                                   FRAME_ServerDh *dh, uint32_t *parseLen)
652 {
653     uint32_t offset = 0;
654     ParseFieldInteger16(&buffer[0], bufLen, &dh->plen, &offset);
655     ParseFieldArray8(&buffer[offset], bufLen - offset, &dh->p, dh->plen.data, &offset);
656     ParseFieldInteger16(&buffer[offset], bufLen - offset, &dh->glen, &offset);
657     ParseFieldArray8(&buffer[offset], bufLen - offset, &dh->g, dh->glen.data, &offset);
658     ParseFieldInteger16(&buffer[offset], bufLen - offset, &dh->pubKeyLen, &offset);
659     ParseFieldArray8(&buffer[offset], bufLen - offset, &dh->pubKey, dh->pubKeyLen.data, &offset);
660     if (((!IS_DTLS_VERSION(frameType->versionType)) && (frameType->versionType >= HITLS_VERSION_TLS12)) ||
661         ((IS_DTLS_VERSION(frameType->versionType)) && (frameType->versionType <= HITLS_VERSION_DTLS12))) {
662         ParseFieldInteger16(&buffer[offset], bufLen - offset, &dh->signAlgorithm, &offset);
663     }
664     ParseFieldInteger16(&buffer[offset], bufLen - offset, &dh->signSize, &offset);
665     ParseFieldArray8(&buffer[offset], bufLen - offset, &dh->signData, dh->signSize.data, &offset);
666     *parseLen += offset;
667     return HITLS_SUCCESS;
668 }
669 
ParseServerKxEccMsg(const uint8_t * buffer,uint32_t bufLen,FRAME_ServerEcdh * ecdh,uint32_t * parseLen)670 static int32_t ParseServerKxEccMsg(const uint8_t *buffer, uint32_t bufLen,
671                                     FRAME_ServerEcdh *ecdh, uint32_t *parseLen)
672 {
673     uint32_t offset = 0;
674     ParseFieldInteger16(&buffer[offset], bufLen - offset, &ecdh->signSize, &offset);
675     ParseFieldArray8(&buffer[offset], bufLen - offset, &ecdh->signData, ecdh->signSize.data, &offset);
676     *parseLen += offset;
677     return HITLS_SUCCESS;
678 }
679 
ParseServerKxMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_ServerKeyExchangeMsg * serverKx,uint32_t * parseLen)680 static int32_t ParseServerKxMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
681                                 FRAME_ServerKeyExchangeMsg *serverKx, uint32_t *parseLen)
682 {
683     switch (frameType->keyExType) {
684         case HITLS_KEY_EXCH_ECDHE:
685             return ParseServerKxEcdhMsg(frameType, buffer, bufLen, &serverKx->keyEx.ecdh, parseLen);
686         case HITLS_KEY_EXCH_DHE:
687             return ParseServerKxDhMsg(frameType, buffer, bufLen, &serverKx->keyEx.dh, parseLen);
688         case HITLS_KEY_EXCH_ECC:
689             return ParseServerKxEccMsg(buffer, bufLen, &serverKx->keyEx.ecdh, parseLen);
690         default:
691             break;
692     }
693     return HITLS_PARSE_UNSUPPORT_KX_ALG;
694 }
695 
CleanServerKxMsg(HITLS_KeyExchAlgo kexType,FRAME_ServerKeyExchangeMsg * serverKx)696 static void CleanServerKxMsg(HITLS_KeyExchAlgo kexType, FRAME_ServerKeyExchangeMsg *serverKx)
697 {
698     FRAME_ServerEcdh *ecdh = &serverKx->keyEx.ecdh;
699     FRAME_ServerDh *dh = &serverKx->keyEx.dh;
700     switch (kexType) {
701         case HITLS_KEY_EXCH_ECDHE:
702             BSL_SAL_FREE(ecdh->pubKey.data);
703             BSL_SAL_FREE(ecdh->signData.data);
704             break;
705         case HITLS_KEY_EXCH_DHE:
706             BSL_SAL_FREE(dh->p.data);
707             BSL_SAL_FREE(dh->g.data);
708             BSL_SAL_FREE(dh->pubKey.data);
709             BSL_SAL_FREE(dh->signData.data);
710             break;
711         default:
712             break;
713     }
714     return;
715 }
716 
ParseCertReqMsgExBody(uint16_t extMsgType,const uint8_t * buffer,uint32_t bufLen,FRAME_CertificateRequestMsg * certReq,uint32_t * parseLen)717 static int32_t ParseCertReqMsgExBody(uint16_t extMsgType, const uint8_t *buffer, uint32_t bufLen,
718     FRAME_CertificateRequestMsg *certReq, uint32_t *parseLen)
719 {
720     uint32_t offset = 0;
721     switch (extMsgType) {
722         case HS_EX_TYPE_SIGNATURE_ALGORITHMS:
723             ParseFieldInteger16(&buffer[offset], bufLen - offset, &certReq->signatureAlgorithmsSize, &offset);
724             ParseFieldArray16(&buffer[offset], bufLen - offset, &certReq->signatureAlgorithms,
725                                 certReq->signatureAlgorithmsSize.data, &offset);
726             break;
727         default:
728             break;
729     }
730     *parseLen += offset;
731     return HITLS_SUCCESS;
732 }
733 
ParseCertReqMsg(FRAME_Type * type,const uint8_t * buffer,uint32_t bufLen,FRAME_CertificateRequestMsg * certReq,uint32_t * parseLen)734 static int32_t ParseCertReqMsg(
735     FRAME_Type *type, const uint8_t *buffer, uint32_t bufLen, FRAME_CertificateRequestMsg *certReq, uint32_t *parseLen)
736 {
737     uint32_t offset = 0;
738     certReq->state = INITIAL_FIELD;
739     if (type->versionType != HITLS_VERSION_TLS13) {
740         ParseFieldInteger8(&buffer[0], bufLen, &certReq->certTypesSize, &offset);
741         ParseFieldArray8(&buffer[offset], bufLen - offset, &certReq->certTypes, certReq->certTypesSize.data, &offset);
742         ParseFieldInteger16(&buffer[offset], bufLen - offset, &certReq->signatureAlgorithmsSize, &offset);
743         ParseFieldArray16(&buffer[offset], bufLen - offset, &certReq->signatureAlgorithms,
744                         certReq->signatureAlgorithmsSize.data, &offset);
745         ParseFieldInteger16(&buffer[offset], bufLen - offset, &certReq->distinguishedNamesSize, &offset);
746         if (certReq->distinguishedNamesSize.data != 0u) {
747             ParseFieldArray8(&buffer[offset], bufLen - offset, &certReq->distinguishedNames,
748                             certReq->distinguishedNamesSize.data, &offset);
749         }
750     } else {
751         ParseFieldInteger8(&buffer[0], bufLen, &certReq->certificateReqCtxSize, &offset);
752         ParseFieldArray8(&buffer[offset],
753             bufLen - offset,
754             &certReq->certificateReqCtx,
755             certReq->certificateReqCtxSize.data,
756             &offset);
757         ParseFieldInteger16(&buffer[offset], bufLen - offset, &certReq->exMsgLen, &offset);
758         while (offset < bufLen) {
759             uint32_t tmpOffset = offset;
760             FRAME_Integer extMsgType ;
761             FRAME_Integer extMsgLen ;
762             ParseFieldInteger16(&buffer[offset], bufLen - offset, &extMsgType, &offset);
763             ParseFieldInteger16(&buffer[offset], bufLen - offset, &extMsgLen, &offset);
764             ParseCertReqMsgExBody(extMsgType.data, &buffer[offset], bufLen - offset, certReq, &offset);
765             if (offset == tmpOffset) {
766                 break;
767             }
768         }
769     }
770     *parseLen += offset;
771     return HITLS_SUCCESS;
772 }
773 
CleanCertReqMsg(FRAME_CertificateRequestMsg * certReq)774 static void CleanCertReqMsg(FRAME_CertificateRequestMsg *certReq)
775 {
776     BSL_SAL_FREE(certReq->certTypes.data);
777     BSL_SAL_FREE(certReq->signatureAlgorithms.data);
778     BSL_SAL_FREE(certReq->distinguishedNames.data);
779     BSL_SAL_FREE(certReq->certificateReqCtx.data);
780     return;
781 }
782 
ParseServerHelloDoneMsg(uint32_t bufLen)783 static int32_t ParseServerHelloDoneMsg(uint32_t bufLen)
784 {
785     if (bufLen != 0) {
786         return HITLS_PARSE_INVALID_MSG_LEN;
787     }
788     return HITLS_SUCCESS;
789 }
790 
CleanServerHelloDoneMsg(FRAME_ServerHelloDoneMsg * serverHelloDone)791 static void CleanServerHelloDoneMsg(FRAME_ServerHelloDoneMsg *serverHelloDone)
792 {
793     /* The ServerHelloDone packet is an empty packet. If there is any constructed data, release it. */
794     BSL_SAL_FREE(serverHelloDone->extra.data);
795     return;
796 }
797 
ParseClientKxMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_ClientKeyExchangeMsg * clientKx,uint32_t * parseLen)798 static int32_t ParseClientKxMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
799                                 FRAME_ClientKeyExchangeMsg *clientKx, uint32_t *parseLen)
800 {
801     uint32_t offset = 0;
802     switch (frameType->keyExType) {
803         case HITLS_KEY_EXCH_ECDHE:
804             /* Compatible with OpenSSL. Three bytes are added to the client key exchange. */
805 #ifdef HITLS_TLS_PROTO_TLCP11
806             if (frameType->versionType == HITLS_VERSION_TLCP_DTLCP11) {
807                 // Curve type + Curve ID + Public key length
808                 uint8_t minLen = sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint8_t);
809                 if (bufLen < minLen) {
810                     return HITLS_PARSE_INVALID_MSG_LEN;
811                 }
812                 // Ignore the first three bytes.
813                 offset += sizeof(uint8_t) + sizeof(uint16_t);
814             }
815 #endif
816             ParseFieldInteger8(&buffer[offset], bufLen - offset, &clientKx->pubKeySize, &offset);
817             ParseFieldArray8(&buffer[offset], bufLen - offset, &clientKx->pubKey, clientKx->pubKeySize.data, &offset);
818             break;
819         case HITLS_KEY_EXCH_DHE:
820         case HITLS_KEY_EXCH_RSA:
821             ParseFieldInteger16(&buffer[offset], bufLen - offset, &clientKx->pubKeySize, &offset);
822             ParseFieldArray8(&buffer[offset], bufLen - offset, &clientKx->pubKey, clientKx->pubKeySize.data, &offset);
823             break;
824         default:
825             return HITLS_PARSE_UNSUPPORT_KX_ALG;
826     }
827     *parseLen += offset;
828     return HITLS_SUCCESS;
829 }
830 
CleanClientKxMsg(FRAME_ClientKeyExchangeMsg * clientKx)831 static void CleanClientKxMsg(FRAME_ClientKeyExchangeMsg *clientKx)
832 {
833     BSL_SAL_FREE(clientKx->pubKey.data);
834     return;
835 }
836 
ParseCertVerifyMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_CertificateVerifyMsg * certVerify,uint32_t * parseLen)837 static int32_t ParseCertVerifyMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
838                                   FRAME_CertificateVerifyMsg *certVerify, uint32_t *parseLen)
839 {
840     uint32_t offset = 0;
841     if (((!IS_DTLS_VERSION(frameType->versionType)) && (frameType->versionType >= HITLS_VERSION_TLS12)) ||
842         ((IS_DTLS_VERSION(frameType->versionType)) && (frameType->versionType <= HITLS_VERSION_DTLS12))) {
843         ParseFieldInteger16(&buffer[0], bufLen, &certVerify->signHashAlg, &offset);
844     }
845     ParseFieldInteger16(&buffer[offset], bufLen - offset, &certVerify->signSize, &offset);
846     ParseFieldArray8(&buffer[offset], bufLen - offset, &certVerify->sign, certVerify->signSize.data, &offset);
847     *parseLen += offset;
848     return HITLS_SUCCESS;
849 }
850 
CleanCertVerifyMsg(FRAME_CertificateVerifyMsg * certVerify)851 static void CleanCertVerifyMsg(FRAME_CertificateVerifyMsg *certVerify)
852 {
853     BSL_SAL_FREE(certVerify->sign.data);
854     return;
855 }
856 
ParseFinishedMsg(const uint8_t * buffer,uint32_t bufLen,FRAME_FinishedMsg * finished,uint32_t * parseLen)857 static int32_t ParseFinishedMsg(const uint8_t *buffer, uint32_t bufLen, FRAME_FinishedMsg *finished, uint32_t *parseLen)
858 {
859     uint32_t offset = 0;
860     ParseFieldArray8(buffer, bufLen, &finished->verifyData, bufLen, &offset);
861     *parseLen += offset;
862     return HITLS_SUCCESS;
863 }
864 
CleanFinishedMsg(FRAME_FinishedMsg * finished)865 static void CleanFinishedMsg(FRAME_FinishedMsg *finished)
866 {
867     BSL_SAL_FREE(finished->verifyData.data);
868     return;
869 }
870 
ParseNewSessionTicket(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_NewSessionTicketMsg * sessionTicket,uint32_t * parseLen)871 static int32_t ParseNewSessionTicket(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
872                                      FRAME_NewSessionTicketMsg *sessionTicket, uint32_t *parseLen)
873 {
874     uint32_t offset = 0;
875     ParseFieldInteger32(&buffer[0], bufLen, &sessionTicket->ticketLifetime, &offset);
876 
877     if (frameType->versionType != HITLS_VERSION_TLS13) {
878         ParseFieldInteger16(&buffer[offset], bufLen - offset, &sessionTicket->ticketSize, &offset);
879         ParseFieldArray8(
880             &buffer[offset], bufLen - offset, &sessionTicket->ticket, sessionTicket->ticketSize.data, &offset);
881     } else {
882         ParseFieldInteger32(&buffer[offset], bufLen - offset, &sessionTicket->ticketAgeAdd, &offset);
883         ParseFieldInteger8(&buffer[offset], bufLen - offset, &sessionTicket->ticketNonceSize, &offset);
884         ParseFieldArray8(&buffer[offset], bufLen - offset, &sessionTicket->ticketNonce,
885             sessionTicket->ticketNonceSize.data, &offset);
886         ParseFieldInteger16(&buffer[offset], bufLen - offset, &sessionTicket->ticketSize, &offset);
887         ParseFieldArray8(
888             &buffer[offset], bufLen - offset, &sessionTicket->ticket, sessionTicket->ticketSize.data, &offset);
889         ParseFieldInteger16(&buffer[offset], bufLen - offset, &sessionTicket->extensionLen, &offset);
890         while (offset < bufLen) {
891             FRAME_Integer tmpField = {0};
892             uint32_t tmpOffset = offset;
893             ParseFieldInteger16(&buffer[tmpOffset], bufLen - tmpOffset, &tmpField, &tmpOffset);
894             switch (tmpField.data) {
895                 default:
896                     /* The extensions in the tls 1.3 new session ticket cannot be parsed currently. Skip this step. */
897                     ParseFieldInteger16(&buffer[tmpOffset], bufLen - tmpOffset, &tmpField, &tmpOffset);
898                     tmpOffset += tmpField.data;
899                     offset = tmpOffset;
900                     break;
901             }
902         }
903     }
904     *parseLen += offset;
905     return HITLS_SUCCESS;
906 }
907 
CleanNewSessionTicket(FRAME_NewSessionTicketMsg * sessionTicket)908 static void CleanNewSessionTicket(FRAME_NewSessionTicketMsg *sessionTicket)
909 {
910     BSL_SAL_FREE(sessionTicket->ticket.data);
911     BSL_SAL_FREE(sessionTicket->ticketNonce.data);
912     return;
913 }
914 
ParseHsMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_HsMsg * hsMsg,uint32_t * parseLen)915 static int32_t ParseHsMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
916     FRAME_HsMsg *hsMsg, uint32_t *parseLen)
917 {
918     uint32_t offset = 0;
919     ParseFieldInteger8(&buffer[0], bufLen, &hsMsg->type, &offset);
920     ParseFieldInteger24(&buffer[offset], bufLen - offset, &hsMsg->length, &offset);
921 
922     if (IS_TRANSTYPE_DATAGRAM(frameType->transportType)) {
923         ParseFieldInteger16(&buffer[offset], bufLen - offset, &hsMsg->sequence, &offset);
924         ParseFieldInteger24(&buffer[offset], bufLen - offset, &hsMsg->fragmentOffset, &offset);
925         ParseFieldInteger24(&buffer[offset], bufLen - offset, &hsMsg->fragmentLength, &offset);
926     }
927     *parseLen += offset;
928 
929     /* To ensure that the memory can be normally released after users modify hsMsg->type.data,
930      * assign a value to frameType. */
931     frameType->handshakeType = hsMsg->type.data;
932     switch (hsMsg->type.data) {
933         case CLIENT_HELLO:
934             return ParseClientHelloMsg(frameType, &buffer[offset], bufLen - offset, &hsMsg->body.clientHello, parseLen);
935         case SERVER_HELLO:
936             return ParseServerHelloMsg(&buffer[offset], bufLen - offset, &hsMsg->body.serverHello, parseLen);
937         case CERTIFICATE:
938             return ParseCertificateMsg(frameType, &buffer[offset], bufLen - offset, &hsMsg->body.certificate, parseLen);
939         case SERVER_KEY_EXCHANGE:
940             return ParseServerKxMsg(frameType, &buffer[offset], bufLen - offset, &hsMsg->body.serverKeyExchange,
941                                     parseLen);
942         case CERTIFICATE_REQUEST:
943             return ParseCertReqMsg(frameType, &buffer[offset], bufLen - offset, &hsMsg->body.certificateReq, parseLen);
944         case CLIENT_KEY_EXCHANGE:
945             return ParseClientKxMsg(frameType, &buffer[offset], bufLen - offset, &hsMsg->body.clientKeyExchange,
946                                     parseLen);
947         case CERTIFICATE_VERIFY:
948             return ParseCertVerifyMsg(frameType, &buffer[offset], bufLen - offset, &hsMsg->body.certificateVerify,
949                                       parseLen);
950         case FINISHED:
951             return ParseFinishedMsg(&buffer[offset], bufLen - offset, &hsMsg->body.finished, parseLen);
952         case SERVER_HELLO_DONE:
953             return ParseServerHelloDoneMsg(bufLen - offset);
954         case NEW_SESSION_TICKET:
955             return ParseNewSessionTicket(frameType, &buffer[offset], bufLen - offset, &hsMsg->body.newSessionTicket,
956                                     parseLen);
957         default:
958             break;
959     }
960     return HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG;
961 }
962 
ParseCcsMsg(const uint8_t * buffer,uint32_t bufLen,FRAME_CcsMsg * ccsMsg,uint32_t * parseLen)963 static int32_t ParseCcsMsg(const uint8_t *buffer, uint32_t bufLen, FRAME_CcsMsg *ccsMsg, uint32_t *parseLen)
964 {
965     /* The length of the CCS message is 1 byte. */
966     if (bufLen != 1u) {
967         return HITLS_PARSE_INVALID_MSG_LEN;
968     }
969 
970     uint32_t offset = 0;
971     ParseFieldInteger8(buffer, bufLen, &ccsMsg->ccsType, &offset);
972     *parseLen += offset;
973     return HITLS_SUCCESS;
974 }
975 
CleanCcsMsg(FRAME_CcsMsg * ccsMsg)976 static void CleanCcsMsg(FRAME_CcsMsg *ccsMsg)
977 {
978     /* This field is used to construct abnormal packets. Data is not written during parsing. However,
979      * users may apply for memory. Therefore, this field needs to be released. */
980     BSL_SAL_FREE(ccsMsg->extra.data);
981     return;
982 }
983 
ParseAlertMsg(const uint8_t * buffer,uint32_t bufLen,FRAME_AlertMsg * alertMsg,uint32_t * parseLen)984 static int32_t ParseAlertMsg(const uint8_t *buffer, uint32_t bufLen, FRAME_AlertMsg *alertMsg, uint32_t *parseLen)
985 {
986     /* The length of the alert message is 2 bytes. */
987     if (bufLen != 2u) {
988         return HITLS_PARSE_INVALID_MSG_LEN;
989     }
990 
991     uint32_t offset = 0;
992     ParseFieldInteger8(&buffer[0], bufLen, &alertMsg->alertLevel, &offset);
993     ParseFieldInteger8(&buffer[offset], bufLen - offset, &alertMsg->alertDescription, &offset);
994     *parseLen += offset;
995     return HITLS_SUCCESS;
996 }
997 
CleanAlertMsg(FRAME_AlertMsg * alertMsg)998 static void CleanAlertMsg(FRAME_AlertMsg *alertMsg)
999 {
1000     /* This field is used to construct abnormal packets. Data is not written during parsing.
1001      * However, users may apply for memory. Therefore, this field needs to be released. */
1002     BSL_SAL_FREE(alertMsg->extra.data);
1003     return;
1004 }
1005 
ParseAppMsg(const uint8_t * buffer,uint32_t bufLen,FRAME_AppMsg * appMsg,uint32_t * parseLen)1006 static int32_t ParseAppMsg(const uint8_t *buffer, uint32_t bufLen, FRAME_AppMsg *appMsg, uint32_t *parseLen)
1007 {
1008     if (bufLen == 0u) {
1009         return HITLS_PARSE_INVALID_MSG_LEN;
1010     }
1011 
1012     uint32_t offset = 0;
1013     ParseFieldArray8(buffer, bufLen, &appMsg->appData, bufLen, &offset);
1014     *parseLen += offset;
1015     return HITLS_SUCCESS;
1016 }
1017 
CleanAppMsg(FRAME_AppMsg * appMsg)1018 static void CleanAppMsg(FRAME_AppMsg *appMsg)
1019 {
1020     BSL_SAL_FREE(appMsg->appData.data);
1021     return;
1022 }
1023 
FRAME_ParseMsgHeader(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_Msg * msg,uint32_t * parseLen)1024 int32_t FRAME_ParseMsgHeader(
1025     FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen, FRAME_Msg *msg, uint32_t *parseLen)
1026 {
1027     if ((frameType == NULL) || (buffer == NULL) || (msg == NULL) || (parseLen == NULL)) {
1028         return HITLS_INTERNAL_EXCEPTION;
1029     }
1030 
1031     uint32_t offset = 0;
1032     ParseFieldInteger8(&buffer[0], bufLen, &msg->recType, &offset);
1033     ParseFieldInteger16(&buffer[offset], bufLen - offset, &msg->recVersion, &offset);
1034 
1035     if (IS_TRANSTYPE_DATAGRAM(frameType->transportType)) {
1036         ParseFieldInteger16(&buffer[offset], bufLen - offset, &msg->epoch, &offset);
1037         ParseFieldInteger48(&buffer[offset], bufLen - offset, &msg->sequence, &offset);
1038     }
1039 
1040     ParseFieldInteger16(&buffer[offset], bufLen - offset, &msg->length, &offset);
1041     if ((msg->length.data + offset) > bufLen) {
1042         return HITLS_PARSE_INVALID_MSG_LEN;
1043     }
1044 
1045     *parseLen = offset;
1046     return HITLS_SUCCESS;
1047 }
1048 
FRAME_ParseTLSRecordHeader(const uint8_t * buffer,uint32_t bufferLen,FRAME_Msg * msg,uint32_t * parsedLen)1049 int32_t FRAME_ParseTLSRecordHeader(const uint8_t *buffer, uint32_t bufferLen, FRAME_Msg *msg, uint32_t *parsedLen)
1050 {
1051     if ((buffer == NULL) || (msg == NULL) || (parsedLen == NULL)) {
1052         return HITLS_INTERNAL_EXCEPTION;
1053     }
1054 
1055     uint32_t offset = 0;
1056     // Parse the 0th byte of the buffer. The parsing result is stored in msg->recType, offset = 1.
1057     ParseFieldInteger8(buffer, bufferLen, &msg->recType, &offset);
1058     // Parse the first and second bytes of the buffer. The parsing result is stored in msg->recVersion, offset = 3.
1059     ParseFieldInteger16(buffer + offset, bufferLen - offset, &msg->recVersion, &offset);
1060     // Parse the third to fourth bytes of the buffer. The parsing result is stored in msg->length, and offset = 5.
1061     ParseFieldInteger16(buffer + offset, bufferLen - offset, &msg->length, &offset);
1062     // msg->length.data indicates the length of the parsed record body.
1063     // In this case, the value of offset is the header length.
1064     if ((msg->length.data + offset) > bufferLen) {
1065         // The length of the entire message cannot be greater than bufLen.
1066         return HITLS_PARSE_INVALID_MSG_LEN;
1067     }
1068 
1069     *parsedLen = offset;
1070     return HITLS_SUCCESS;
1071 }
1072 
1073 // Parse the body of a non-handshake record.
FRAME_ParseTLSNonHsRecordBody(const uint8_t * buffer,uint32_t bufferLen,FRAME_Msg * msg,uint32_t * parseLen)1074 int32_t FRAME_ParseTLSNonHsRecordBody(const uint8_t *buffer, uint32_t bufferLen, FRAME_Msg *msg, uint32_t *parseLen)
1075 {
1076     if ((buffer == NULL) || (msg == NULL) || (parseLen == NULL)) {
1077         return HITLS_INTERNAL_EXCEPTION;
1078     }
1079 
1080     switch (msg->recType.data) {
1081         case REC_TYPE_CHANGE_CIPHER_SPEC:
1082             return ParseCcsMsg(buffer, bufferLen, &msg->body.ccsMsg, parseLen);
1083         case REC_TYPE_ALERT:
1084             return ParseAlertMsg(buffer, bufferLen, &msg->body.alertMsg, parseLen);
1085         case REC_TYPE_APP:
1086             return ParseAppMsg(buffer, bufferLen, &msg->body.appMsg, parseLen);
1087         default:
1088             break;
1089     }
1090     return HITLS_INTERNAL_EXCEPTION;
1091 }
1092 
FRAME_ParseMsgBody(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_Msg * msg,uint32_t * parseLen)1093 int32_t FRAME_ParseMsgBody(
1094     FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen, FRAME_Msg *msg, uint32_t *parseLen)
1095 {
1096     if ((frameType == NULL) || (buffer == NULL) || (msg == NULL) || (parseLen == NULL)) {
1097         return HITLS_INTERNAL_EXCEPTION;
1098     }
1099 
1100     /* To ensure that the memory can be normally released after users modify msg->recType.data,
1101      * assign a value to frameType. */
1102     frameType->recordType = msg->recType.data;
1103     switch (msg->recType.data) {
1104         case REC_TYPE_HANDSHAKE:
1105             return ParseHsMsg(frameType, buffer, bufLen, &msg->body.hsMsg, parseLen);
1106         case REC_TYPE_CHANGE_CIPHER_SPEC:
1107             return ParseCcsMsg(buffer, bufLen, &msg->body.ccsMsg, parseLen);
1108         case REC_TYPE_ALERT:
1109             return ParseAlertMsg(buffer, bufLen, &msg->body.alertMsg, parseLen);
1110         case REC_TYPE_APP:
1111             return ParseAppMsg(buffer, bufLen, &msg->body.appMsg, parseLen);
1112         default:
1113             break;
1114     }
1115 
1116     return HITLS_INTERNAL_EXCEPTION;
1117 }
1118 
FRAME_ParseMsg(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufLen,FRAME_Msg * msg,uint32_t * parseLen)1119 int32_t FRAME_ParseMsg(FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufLen,
1120                        FRAME_Msg *msg, uint32_t *parseLen)
1121 {
1122     int32_t ret;
1123     ret = FRAME_ParseMsgHeader(frameType, buffer, bufLen, msg, parseLen);
1124     if (ret != HITLS_SUCCESS) {
1125         return ret;
1126     }
1127 
1128     return FRAME_ParseMsgBody(frameType, &buffer[*parseLen], msg->length.data, msg, parseLen);
1129 }
1130 
FRAME_ParseTLSNonHsRecord(const uint8_t * buffer,uint32_t bufLen,FRAME_Msg * msg,uint32_t * parseLen)1131 int32_t FRAME_ParseTLSNonHsRecord(const uint8_t *buffer, uint32_t bufLen, FRAME_Msg *msg, uint32_t *parseLen)
1132 {
1133     int32_t ret;
1134     uint32_t headerLen;
1135     ret = FRAME_ParseTLSRecordHeader(buffer, bufLen, msg, parseLen);
1136     if (ret != HITLS_SUCCESS) {
1137         return ret;
1138     }
1139     headerLen = *parseLen;
1140     return FRAME_ParseTLSNonHsRecordBody(buffer + headerLen, msg->length.data, msg, parseLen);
1141 }
1142 
FRAME_ParseHsRecord(FRAME_Type * frameType,const uint8_t * buffer,uint32_t bufferLen,FRAME_Msg * msg,uint32_t * parseLen)1143 int32_t FRAME_ParseHsRecord(
1144     FRAME_Type *frameType, const uint8_t *buffer, uint32_t bufferLen, FRAME_Msg *msg, uint32_t *parseLen)
1145 {
1146     int32_t ret;
1147     uint32_t headerLen;
1148     ret = FRAME_ParseMsgHeader(frameType, buffer, bufferLen, msg, parseLen);
1149     if (ret != HITLS_SUCCESS) {
1150         return ret;
1151     }
1152     headerLen = *parseLen;
1153     /* To ensure that the memory can be normally released after users modify msg->recType.data,
1154      * assign a value to frameType. */
1155     frameType->recordType = msg->recType.data;
1156     if (msg->recType.data == REC_TYPE_HANDSHAKE) {
1157         return ParseHsMsg(frameType, buffer + headerLen, msg->length.data, &msg->body.hsMsg, parseLen);
1158     }
1159     return HITLS_PARSE_UNSUPPORT_HANDSHAKE_MSG;
1160 }
1161 
CleanParsedHsMsg(HS_MsgType handshakeType,HITLS_KeyExchAlgo kexType,FRAME_HsMsg * hsMsg)1162 static void CleanParsedHsMsg(HS_MsgType handshakeType, HITLS_KeyExchAlgo kexType, FRAME_HsMsg *hsMsg)
1163 {
1164     switch (handshakeType) {
1165         case CLIENT_HELLO:
1166             return CleanClientHelloMsg(&hsMsg->body.clientHello);
1167         case SERVER_HELLO:
1168             return CleanServerHelloMsg(&hsMsg->body.serverHello);
1169         case CERTIFICATE:
1170             return CleanCertificateMsg(&hsMsg->body.certificate);
1171         case SERVER_KEY_EXCHANGE:
1172             return CleanServerKxMsg(kexType, &hsMsg->body.serverKeyExchange);
1173         case CERTIFICATE_REQUEST:
1174             return CleanCertReqMsg(&hsMsg->body.certificateReq);
1175         case CLIENT_KEY_EXCHANGE:
1176             return CleanClientKxMsg(&hsMsg->body.clientKeyExchange);
1177         case CERTIFICATE_VERIFY:
1178             return CleanCertVerifyMsg(&hsMsg->body.certificateVerify);
1179         case FINISHED:
1180             return CleanFinishedMsg(&hsMsg->body.finished);
1181         case SERVER_HELLO_DONE:
1182             return CleanServerHelloDoneMsg(&hsMsg->body.serverHelloDone);
1183         case NEW_SESSION_TICKET:
1184             return CleanNewSessionTicket(&hsMsg->body.newSessionTicket);
1185         default:
1186             break;
1187     }
1188     return;
1189 }
1190 
CleanHsMsg(FRAME_Type * frameType,FRAME_HsMsg * hsMsg)1191 static void CleanHsMsg(FRAME_Type *frameType, FRAME_HsMsg *hsMsg)
1192 {
1193     return CleanParsedHsMsg(frameType->handshakeType, frameType->keyExType, hsMsg);
1194 }
1195 
FRAME_CleanMsg(FRAME_Type * frameType,FRAME_Msg * msg)1196 void FRAME_CleanMsg(FRAME_Type *frameType, FRAME_Msg *msg)
1197 {
1198     if ((frameType == NULL) || (msg == NULL)) {
1199         return;
1200     }
1201 
1202     switch (frameType->recordType) {
1203         case REC_TYPE_HANDSHAKE:
1204             CleanHsMsg(frameType, &msg->body.hsMsg);
1205             break;
1206         case REC_TYPE_CHANGE_CIPHER_SPEC:
1207             CleanCcsMsg(&msg->body.ccsMsg);
1208             break;
1209         case REC_TYPE_ALERT:
1210             CleanAlertMsg(&msg->body.alertMsg);
1211             break;
1212         case REC_TYPE_APP:
1213             CleanAppMsg(&msg->body.appMsg);
1214             break;
1215         default:
1216             break;
1217     }
1218     memset_s(msg, sizeof(FRAME_Msg), 0, sizeof(FRAME_Msg));
1219     return;
1220 }
1221 
FRAME_CleanNonHsRecord(REC_Type recType,FRAME_Msg * msg)1222 void FRAME_CleanNonHsRecord(REC_Type recType, FRAME_Msg *msg)
1223 {
1224     if (msg == NULL) {
1225         return;
1226     }
1227     switch (recType) {
1228         case REC_TYPE_CHANGE_CIPHER_SPEC:
1229             CleanCcsMsg(&msg->body.ccsMsg);
1230             break;
1231         case REC_TYPE_ALERT:
1232             CleanAlertMsg(&msg->body.alertMsg);
1233             break;
1234         case REC_TYPE_APP:
1235             CleanAppMsg(&msg->body.appMsg);
1236             break;
1237         default:
1238             break;
1239     }
1240     memset_s(msg, sizeof(FRAME_Msg), 0, sizeof(FRAME_Msg));
1241 }