• 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 "hitls_error.h"
19 #include "hitls.h"
20 #include "tls.h"
21 #include "hs_ctx.h"
22 #include "frame_msg.h"
23 #include "hs_extensions.h"
24 
25 #define TLS_RECORD_HEADER_LEN  5
26 #define DTLS_RECORD_HEADER_LEN 13
27 
28 #define SIZE_OF_UINT24 3
29 #define SIZE_OF_UINT32 4
30 #define SIZE_OF_UINT48 6
31 
32 #define ONE_TIME 1
33 #define TWO_TIMES 2
34 
35 // Assemble 8-bit data(1 byte)
PackInteger8(const FRAME_Integer * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)36 static int32_t PackInteger8(const FRAME_Integer *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
37 {
38     uint32_t repeats = ONE_TIME;
39     uint32_t bufOffset = 0;
40 
41     // No assembly required
42     if (field->state == MISSING_FIELD) {
43         return HITLS_SUCCESS;
44     }
45 
46     // Repeated assembly
47     if (field->state == DUPLICATE_FIELD) {
48         repeats = TWO_TIMES;
49     }
50 
51     // Not enough to assemble
52     if (bufLen < (sizeof(uint8_t) * repeats)) {
53         return HITLS_INTERNAL_EXCEPTION;
54     }
55 
56     for (uint32_t i = 0; i < repeats; i++) {
57         uint8_t data = (uint8_t)(field->data);
58         buf[bufOffset] = data;
59         bufOffset += sizeof(uint8_t);
60         *offset += sizeof(uint8_t);
61     }
62     return HITLS_SUCCESS;
63 }
64 
65 // Assemble 16-bit data(2 bytes)
PackInteger16(const FRAME_Integer * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)66 static int32_t PackInteger16(const FRAME_Integer *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
67 {
68     uint32_t repeats = ONE_TIME;
69     uint32_t bufOffset = 0;
70 
71     // No assembly required
72     if (field->state == MISSING_FIELD) {
73         return HITLS_SUCCESS;
74     }
75 
76     // Repeated assembly
77     if (field->state == DUPLICATE_FIELD) {
78         repeats = TWO_TIMES;
79     }
80 
81     // Not enough to assemble
82     if (bufLen < (sizeof(uint16_t) * repeats)) {
83         return HITLS_INTERNAL_EXCEPTION;
84     }
85 
86     if (field->state == SET_LEN_TO_ONE_BYTE) {
87         uint8_t data = (uint8_t)field->data;
88         buf[0] = data;
89         *offset += sizeof(uint8_t);
90         return HITLS_SUCCESS;
91     }
92 
93     for (uint32_t i = 0; i < repeats; i++) {
94         uint16_t data = (uint16_t)(field->data);
95         BSL_Uint16ToByte(data, &buf[bufOffset]);
96         bufOffset += sizeof(uint16_t);
97         *offset += sizeof(uint16_t);
98     }
99     return HITLS_SUCCESS;
100 }
101 
102 // Assemble 24-bit data(3 bytes)
PackInteger24(const FRAME_Integer * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)103 static int32_t PackInteger24(const FRAME_Integer *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
104 {
105     uint32_t repeats = ONE_TIME;
106     uint32_t bufOffset = 0;
107 
108     // No assembly required
109     if (field->state == MISSING_FIELD) {
110         return HITLS_SUCCESS;
111     }
112 
113     // Repeated assembly
114     if (field->state == DUPLICATE_FIELD) {
115         repeats = TWO_TIMES;
116     }
117 
118     // Not enough to assemble
119     if (bufLen < (SIZE_OF_UINT24 * repeats)) {
120         return HITLS_INTERNAL_EXCEPTION;
121     }
122 
123     if (field->state == SET_LEN_TO_ONE_BYTE) {
124         uint8_t data = (uint8_t)field->data;
125         buf[0] = data;
126         *offset += sizeof(uint8_t);
127         return HITLS_SUCCESS;
128     }
129 
130     for (uint32_t i = 0; i < repeats; i++) {
131         uint32_t data = (uint32_t)field->data;
132         BSL_Uint24ToByte(data, &buf[bufOffset]);
133         bufOffset += SIZE_OF_UINT24;
134         *offset += SIZE_OF_UINT24;
135     }
136     return HITLS_SUCCESS;
137 }
138 
139 
140 // Assemble 32-bit data(8 bytes)
PackInteger32(const FRAME_Integer * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)141 static int32_t PackInteger32(const FRAME_Integer *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
142 {
143     uint32_t repeats = ONE_TIME;
144     uint32_t bufOffset = 0;
145 
146     // No assembly required
147     if (field->state == MISSING_FIELD) {
148         return HITLS_SUCCESS;
149     }
150 
151     // Repeated assembly
152     if (field->state == DUPLICATE_FIELD) {
153         repeats = TWO_TIMES;
154     }
155 
156     // Not enough to assemble
157     if (bufLen < (SIZE_OF_UINT32 * repeats)) {
158         return HITLS_INTERNAL_EXCEPTION;
159     }
160 
161     if (field->state == SET_LEN_TO_ONE_BYTE) {
162         uint8_t data = (uint8_t)field->data;
163         buf[0] = data;
164         *offset += sizeof(uint8_t);
165         return HITLS_SUCCESS;
166     }
167 
168     for (uint32_t i = 0; i < repeats; i++) {
169         uint32_t data = (uint32_t)field->data;
170         BSL_Uint32ToByte(data, &buf[bufOffset]);
171         bufOffset += SIZE_OF_UINT32;
172         *offset += SIZE_OF_UINT32;
173     }
174     return HITLS_SUCCESS;
175 }
176 
177 // Assemble 48-bit data(8 bytes)
PackInteger48(const FRAME_Integer * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)178 static int32_t PackInteger48(const FRAME_Integer *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
179 {
180     uint32_t repeats = ONE_TIME;
181     uint32_t bufOffset = 0;
182 
183     // No assembly required
184     if (field->state == MISSING_FIELD) {
185         return HITLS_SUCCESS;
186     }
187 
188     // Repeated assembly
189     if (field->state == DUPLICATE_FIELD) {
190         repeats = TWO_TIMES;
191     }
192 
193     // Not enough to assemble
194     if (bufLen < (SIZE_OF_UINT48 * repeats)) {
195         return HITLS_INTERNAL_EXCEPTION;
196     }
197 
198     if (field->state == SET_LEN_TO_ONE_BYTE) {
199         uint8_t data = (uint8_t)field->data;
200         buf[0] = data;
201         *offset += sizeof(uint8_t);
202         return HITLS_SUCCESS;
203     }
204 
205     for (uint32_t i = 0; i < repeats; i++) {
206         uint64_t data = (uint64_t)field->data;
207         BSL_Uint48ToByte(data, &buf[bufOffset]);
208         bufOffset += SIZE_OF_UINT48;
209         *offset += SIZE_OF_UINT48;
210     }
211     return HITLS_SUCCESS;
212 }
213 
214 // Assembles the buffer of 8-bit data.(1 byte * n)
PackArray8(const FRAME_Array8 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)215 static int32_t PackArray8(const FRAME_Array8 *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
216 {
217     // No assembly required
218     if (field->state == MISSING_FIELD) {
219         return HITLS_SUCCESS;
220     }
221 
222     // Total length to be assembled
223     uint32_t length = field->size;
224 
225     // Not enough to assemble
226     if (bufLen < length) {
227         return HITLS_INTERNAL_EXCEPTION;
228     }
229 
230     if (memcpy_s(buf, bufLen, field->data, field->size) != EOK) {
231         return HITLS_MEMCPY_FAIL;
232     }
233 
234     *offset += length;
235     return HITLS_SUCCESS;
236 }
237 
238 // Assemble the buffer of 16-bit data.(2 bytes * n)
PackArray16(const FRAME_Array16 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)239 static int32_t PackArray16(const FRAME_Array16 *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
240 {
241     // No assembly required
242     if (field->state == MISSING_FIELD) {
243         return HITLS_SUCCESS;
244     }
245 
246    // Total length to be assembled
247     uint32_t length = field->size * sizeof(uint16_t);
248 
249     // Not enough to assemble
250     if (bufLen < length) {
251         return HITLS_INTERNAL_EXCEPTION;
252     }
253 
254     uint32_t bufoffset = 0;
255     for (uint32_t i = 0; i < field->size; i++) {
256         BSL_Uint16ToByte(field->data[i], &buf[bufoffset]);
257         bufoffset += sizeof(uint16_t);
258     }
259 
260     *offset += length;
261     return HITLS_SUCCESS;
262 }
263 
PackHsExtArray8(const FRAME_HsExtArray8 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)264 static int32_t PackHsExtArray8(const FRAME_HsExtArray8 *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
265 {
266     uint32_t repeats = ONE_TIME;
267 
268     // This extension does not need to be assembled.
269     if (field->exState == MISSING_FIELD) {
270         return HITLS_SUCCESS;
271     }
272 
273     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
274     if (field->exState == DUPLICATE_FIELD) {
275         repeats = TWO_TIMES;
276     }
277 
278     // Calculate the total length to be assembled
279     uint32_t length = 0;
280     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
281     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
282     length += ((field->exDataLen.state == MISSING_FIELD) ? 0 : sizeof(uint8_t));
283     length += ((field->exData.state == MISSING_FIELD) ? 0 : sizeof(uint8_t) * field->exData.size);
284     length *= repeats;
285 
286     // Not enough to assemble
287     if (bufLen < length) {
288         return HITLS_INTERNAL_EXCEPTION;
289     }
290 
291     // Assembly extension type. Duplicate extensions exist. Currently, assembly is performed twice consecutively.
292     uint32_t bufoffset = 0;
293     uint32_t tmpOffset;
294     for (uint32_t i = 0; i < repeats; i++) {
295         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
296         tmpOffset = bufoffset;
297         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
298         PackInteger8(&field->exDataLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
299         PackArray8(&field->exData, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
300         if (field->exLen.state == INITIAL_FIELD) {
301             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
302             BSL_Uint16ToByte(len, &buf[tmpOffset]);
303         }
304     }
305     *offset += bufoffset;
306     return HITLS_SUCCESS;
307 }
308 
PackHsExtArrayForList(const FRAME_HsExtArray8 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)309 static int32_t PackHsExtArrayForList(const FRAME_HsExtArray8 *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
310 {
311     uint32_t repeats = ONE_TIME;
312 
313     // This extension does not need to be assembled.
314     if (field->exState == MISSING_FIELD) {
315         return HITLS_SUCCESS;
316     }
317 
318     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
319     if (field->exState == DUPLICATE_FIELD) {
320         repeats = TWO_TIMES;
321     }
322 
323     // Calculate the total length to be assembled
324     uint32_t length = 0;
325     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
326     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
327     length += ((field->exDataLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
328     length += ((field->exData.state == MISSING_FIELD) ? 0 : sizeof(uint8_t) * field->exData.size);
329     length *= repeats;
330 
331     // Not enough to assemble
332     if (bufLen < length) {
333         return HITLS_INTERNAL_EXCEPTION;
334     }
335 
336     // Assembly extension type. Duplicate extensions exist. Currently, assembly is performed twice consecutively.
337     uint32_t bufoffset = 0;
338     uint32_t tmpOffset;
339     for (uint32_t i = 0; i < repeats; i++) {
340         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
341         tmpOffset = bufoffset;
342         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
343         PackInteger16(&field->exDataLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
344         PackArray8(&field->exData, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
345         if (field->exLen.state == INITIAL_FIELD) {
346             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
347             BSL_Uint16ToByte(len, &buf[tmpOffset]);
348         }
349     }
350     *offset += bufoffset;
351     return HITLS_SUCCESS;
352 }
353 
354 
PackHsExtArrayForTicket(const FRAME_HsExtArray8 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)355 static int32_t PackHsExtArrayForTicket(const FRAME_HsExtArray8 *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
356 {
357     uint32_t repeats = ONE_TIME;
358 
359     // This extension does not need to be assembled.
360     if (field->exState == MISSING_FIELD) {
361         return HITLS_SUCCESS;
362     }
363 
364     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
365     if (field->exState == DUPLICATE_FIELD) {
366         repeats = TWO_TIMES;
367     }
368 
369     // Calculate the total length to be assembled
370     uint32_t length = 0;
371     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
372     length += ((field->exDataLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
373     length += ((field->exData.state == MISSING_FIELD) ? 0 : sizeof(uint8_t) * field->exData.size);
374     length *= repeats;
375 
376     // Not enough to assemble
377     if (bufLen < length) {
378         return HITLS_INTERNAL_EXCEPTION;
379     }
380 
381     // Assembly extension type. Duplicate extensions exist. Currently, assembly is performed twice consecutively.
382     uint32_t bufoffset = 0;
383     uint32_t tmpOffset;
384     for (uint32_t i = 0; i < repeats; i++) {
385         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
386         tmpOffset = bufoffset;
387         PackInteger16(&field->exDataLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
388         PackArray8(&field->exData, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
389         if (field->exDataLen.state == INITIAL_FIELD) {
390             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
391             BSL_Uint16ToByte(len, &buf[tmpOffset]);
392         }
393     }
394     *offset += bufoffset;
395     return HITLS_SUCCESS;
396 }
397 
PackHsExtArray16(const FRAME_HsExtArray16 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)398 static int32_t PackHsExtArray16(const FRAME_HsExtArray16 *field, uint8_t *buf,
399     uint32_t bufLen, uint32_t *offset)
400 {
401     uint32_t repeats = ONE_TIME;
402 
403     // This extension does not need to be assembled.
404     if (field->exState == MISSING_FIELD) {
405         return HITLS_SUCCESS;
406     }
407 
408     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
409     if (field->exState == DUPLICATE_FIELD) {
410         repeats = TWO_TIMES;
411     }
412 
413     // Calculate the total length to be assembled
414     uint32_t length = 0;
415     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
416     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
417     length += ((field->exDataLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
418     length += ((field->exData.state == MISSING_FIELD) ? 0 : sizeof(uint16_t) * field->exData.size);
419     length *= repeats;
420 
421     // Not enough to assemble
422     if (bufLen < length) {
423         return HITLS_INTERNAL_EXCEPTION;
424     }
425 
426     // Assembly extension type. Duplicate extensions exist. Currently, assembly is performed twice consecutively.
427     uint32_t bufoffset = 0;
428     uint32_t tmpOffset;
429     for (uint32_t i = 0; i < repeats; i++) {
430         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
431         tmpOffset = bufoffset;
432         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
433         PackInteger16(&field->exDataLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
434         PackArray16(&field->exData, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
435 
436         if (field->exLen.state == INITIAL_FIELD) {
437             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
438             BSL_Uint16ToByte(len, &buf[tmpOffset]);
439         }
440     }
441 
442     *offset += bufoffset;
443     return HITLS_SUCCESS;
444 }
445 
PackPskIdentity(const FRAME_HsArrayPskIdentity * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)446 static int32_t PackPskIdentity(const FRAME_HsArrayPskIdentity *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
447 {
448     // This extension does not need to be assembled.
449     if (field->state == MISSING_FIELD) {
450         return HITLS_SUCCESS;
451     }
452     // Duplicate identity arrays are meaningless. The configuration value can be duplicated.
453 
454     uint32_t bufoffset = 0;
455     uint32_t tmpOffset = 0;
456     for (uint32_t j = 0; j < field->size; j++) {
457         uint32_t innerRepeat = ONE_TIME;
458         if (field->data[j].state == MISSING_FIELD) {
459             continue;
460         }
461         if (field->data[j].state == DUPLICATE_FIELD) {
462             innerRepeat = TWO_TIMES;
463         }
464         for (uint32_t k = 0; k < innerRepeat; k++) {
465             tmpOffset = bufoffset;
466             PackInteger16(&field->data[j].identityLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
467             PackArray8(&field->data[j].identity, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
468             if (field->data[j].identityLen.state == INITIAL_FIELD) {
469                 BSL_Uint16ToByte(field->data[j].identity.size, &buf[tmpOffset]);
470             }
471             PackInteger32(&field->data[j].obfuscatedTicketAge, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
472         }
473     }
474     *offset += bufoffset;
475     return HITLS_SUCCESS;
476 }
477 
PackPskBinder(const FRAME_HsArrayPskBinder * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)478 static int32_t PackPskBinder(const FRAME_HsArrayPskBinder *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
479 {
480     // This extension does not need to be assembled.
481     if (field->state == MISSING_FIELD) {
482         return HITLS_SUCCESS;
483     }
484     // Duplicate identity arrays are meaningless. The configuration value can be duplicated.
485 
486     uint32_t bufoffset = 0;
487     uint32_t tmpOffset = 0;
488     for (uint32_t j = 0; j < field->size; j++) {
489         uint32_t innerRepeat = ONE_TIME;
490         if (field->data[j].state == MISSING_FIELD) {
491             continue;
492         }
493         if (field->data[j].state == DUPLICATE_FIELD) {
494             innerRepeat = TWO_TIMES;
495         }
496         for (uint32_t k = 0; k < innerRepeat; k++) {
497             tmpOffset = bufoffset;
498             PackInteger8(&field->data[j].binderLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
499             PackArray8(&field->data[j].binder, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
500             if (field->data[j].binderLen.state == INITIAL_FIELD) {
501                 buf[tmpOffset] = field->data[j].binder.size;
502             }
503         }
504     }
505     *offset += bufoffset;
506     return HITLS_SUCCESS;
507 }
PackHsExtCaList(const FRAME_HsExtCaList * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)508 static int32_t PackHsExtCaList(const FRAME_HsExtCaList *field, uint8_t *buf,
509     uint32_t bufLen, uint32_t *offset)
510 {
511     if (field->exState == MISSING_FIELD) {
512         return HITLS_SUCCESS;
513     }
514 
515     // Calculate the total length to be assembled
516     uint32_t length = 0;
517     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
518     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
519     length += ((field->listSize.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
520     length += ((field->list.state == MISSING_FIELD) ? 0 : sizeof(uint8_t) * field->list.size);
521 
522     if (bufLen < length) {
523         return HITLS_INTERNAL_EXCEPTION;
524     }
525 
526     uint32_t bufoffset = 0;
527     uint32_t tmpOffset = 0;
528     PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
529     tmpOffset = bufoffset;
530     PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
531     PackInteger16(&field->listSize, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
532     PackArray8(&field->list, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
533 
534     if (field->exLen.state == INITIAL_FIELD) {
535         uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
536         BSL_Uint16ToByte(len, &buf[tmpOffset]);
537     }
538     *offset += bufoffset;
539     return HITLS_SUCCESS;
540 }
PackHsExtOfferedPsks(const FRAME_HsExtOfferedPsks * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)541 static int32_t PackHsExtOfferedPsks(const FRAME_HsExtOfferedPsks *field, uint8_t *buf,
542     uint32_t bufLen, uint32_t *offset)
543 {
544     uint32_t repeats = ONE_TIME;
545 
546     // This extension does not need to be assembled.
547     if (field->exState == MISSING_FIELD) {
548         return HITLS_SUCCESS;
549     }
550 
551     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
552     if (field->exState == DUPLICATE_FIELD) {
553         repeats = TWO_TIMES;
554     }
555 
556     // Calculate the total length to be assembled
557     uint32_t length = 0;
558     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
559     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
560     length += field->exLen.data;
561     length *= repeats;
562 
563     // Not enough to assemble
564     if (bufLen < length) {
565         return HITLS_INTERNAL_EXCEPTION;
566     }
567 
568     uint32_t bufoffset = 0;
569     uint32_t tmpOffset = 0;
570     for (uint32_t i = 0; i < repeats; i++) {
571         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
572         tmpOffset = bufoffset;
573         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
574         PackInteger16(&field->identitySize, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
575         // identity len INITIAL_FIELD Not supported currently
576         PackPskIdentity(&field->identities, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
577         PackInteger16(&field->binderSize, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
578         // binder len INITIAL_FIELD Not supported currently
579         PackPskBinder(&field->binders, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
580         if (field->exLen.state == INITIAL_FIELD) {
581             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
582             BSL_Uint16ToByte(len, &buf[tmpOffset]);
583         }
584     }
585     *offset += bufoffset;
586     return HITLS_SUCCESS;
587 }
PackKeyShareArray(const FRAME_HsArrayKeyShare * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)588 static int32_t PackKeyShareArray(const FRAME_HsArrayKeyShare *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
589 {
590     // This extension does not need to be assembled.
591     if (field->state == MISSING_FIELD) {
592         return HITLS_SUCCESS;
593     }
594     // Duplicate key share arrays are meaningless. The configuration value can be duplicated.
595 
596     uint32_t bufoffset = 0;
597     uint32_t tmpOffset = 0;
598     for (uint32_t j = 0; j < field->size; j++) {
599         uint32_t innerRepeat = ONE_TIME;
600         if (field->data[j].state == MISSING_FIELD) {
601             continue;
602         }
603         if (field->data[j].state == DUPLICATE_FIELD) {
604             innerRepeat = TWO_TIMES;
605         }
606         for (uint32_t k = 0; k < innerRepeat; k++) {
607             PackInteger16(&field->data[j].group, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
608             tmpOffset = bufoffset;
609             PackInteger16(&field->data[j].keyExchangeLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
610             PackArray8(&field->data[j].keyExchange, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
611             if (field->data[j].keyExchangeLen.state == INITIAL_FIELD) {
612                 BSL_Uint16ToByte(field->data[j].keyExchange.size, &buf[tmpOffset]);
613             }
614         }
615     }
616     *offset += bufoffset;
617     return HITLS_SUCCESS;
618 
619 }
PackHsExtKeyShare(const FRAME_HsExtKeyShare * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)620 static int32_t PackHsExtKeyShare(const FRAME_HsExtKeyShare *field, uint8_t *buf,
621     uint32_t bufLen, uint32_t *offset)
622 {
623     uint32_t repeats = ONE_TIME;
624 
625     // This extension does not need to be assembled.
626     if (field->exState == MISSING_FIELD) {
627         return HITLS_SUCCESS;
628     }
629 
630     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
631     if (field->exState == DUPLICATE_FIELD) {
632         repeats = TWO_TIMES;
633     }
634 
635     // Calculate the total length to be assembled
636     uint32_t length = 0;
637     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
638     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
639     length += field->exLen.data;
640     length *= repeats;
641 
642     // Not enough to assemble
643     if (bufLen < length) {
644         return HITLS_INTERNAL_EXCEPTION;
645     }
646 
647     uint32_t bufoffset = 0;
648     uint32_t tmpOffset = 0;
649     for (uint32_t i = 0; i < repeats; i++) {
650         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
651         tmpOffset = bufoffset;
652         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
653         PackInteger16(&field->exKeyShareLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
654         // exKeyShareLen INITIAL_FIELD Not supported currently
655         PackKeyShareArray(&field->exKeyShares, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
656         if (field->exLen.state == INITIAL_FIELD) {
657             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
658             BSL_Uint16ToByte(len, &buf[tmpOffset]);
659         }
660     }
661     *offset += bufoffset;
662     return HITLS_SUCCESS;
663 }
664 
PackHsExtSupportedVersion(const FRAME_HsExtArray16 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)665 static int32_t PackHsExtSupportedVersion(const FRAME_HsExtArray16 *field, uint8_t *buf,
666     uint32_t bufLen, uint32_t *offset)
667 {
668     uint32_t repeats = ONE_TIME;
669 
670     // This extension does not need to be assembled.
671     if (field->exState == MISSING_FIELD) {
672         return HITLS_SUCCESS;
673     }
674 
675     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
676     if (field->exState == DUPLICATE_FIELD) {
677         repeats = TWO_TIMES;
678     }
679 
680     // Calculate the total length to be assembled
681     uint32_t length = 0;
682     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
683     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
684     length += ((field->exDataLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
685     length += ((field->exData.state == MISSING_FIELD) ? 0 : sizeof(uint16_t) * field->exData.size);
686     length *= repeats;
687 
688     // Not enough to assemble
689     if (bufLen < length) {
690         return HITLS_INTERNAL_EXCEPTION;
691     }
692 
693     // Assembly extension type. Duplicate extensions exist. Currently, assembly is performed twice consecutively.
694     uint32_t bufoffset = 0;
695     uint32_t tmpOffset;
696     for (uint32_t i = 0; i < repeats; i++) {
697         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
698         tmpOffset = bufoffset;
699         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
700         PackInteger8(&field->exDataLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
701         PackArray16(&field->exData, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
702         if (field->exLen.state == INITIAL_FIELD) {
703             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
704             BSL_Uint16ToByte(len, &buf[tmpOffset]);
705         }
706     }
707 
708     *offset += bufoffset;
709     return HITLS_SUCCESS;
710 }
711 
PackClientHelloMsg(const FRAME_ClientHelloMsg * clientHello,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)712 static int32_t PackClientHelloMsg(const FRAME_ClientHelloMsg *clientHello, uint8_t *buf,
713     uint32_t bufLen, uint32_t *usedLen)
714 {
715     uint32_t offset = 0;
716     uint32_t bufOffset;
717 
718     PackInteger16(&clientHello->version, &buf[offset], bufLen, &offset);
719     PackArray8(&clientHello->randomValue, &buf[offset], bufLen - offset, &offset);
720     PackInteger8(&clientHello->sessionIdSize, &buf[offset], bufLen - offset, &offset);
721     PackArray8(&clientHello->sessionId, &buf[offset], bufLen - offset, &offset);
722     PackInteger8(&clientHello->cookiedLen, &buf[offset], bufLen - offset, &offset);
723     PackArray8(&clientHello->cookie, &buf[offset], bufLen - offset, &offset);
724     PackInteger16(&clientHello->cipherSuitesSize, &buf[offset], bufLen - offset, &offset);
725     PackArray16(&clientHello->cipherSuites, &buf[offset], bufLen - offset, &offset);
726     PackInteger8(&clientHello->compressionMethodsLen, &buf[offset], bufLen - offset, &offset);
727     PackArray8(&clientHello->compressionMethods, &buf[offset], bufLen - offset, &offset);
728 
729     bufOffset = offset;
730     if (clientHello->extensionState != MISSING_FIELD) {
731         PackInteger16(&clientHello->extensionLen, &buf[offset], bufLen - offset, &offset);
732         if (clientHello->extensionLen.state == SET_LEN_TO_ONE_BYTE) {
733             goto EXIT;
734         }
735         PackHsExtArrayForList(&clientHello->serverName, &buf[offset], bufLen - offset, &offset);
736         PackHsExtArray16(&clientHello->signatureAlgorithms, &buf[offset], bufLen - offset, &offset);
737         PackHsExtArray16(&clientHello->supportedGroups, &buf[offset], bufLen - offset, &offset);
738         PackHsExtArray8(&clientHello->pointFormats, &buf[offset], bufLen - offset, &offset);
739         PackHsExtSupportedVersion(&clientHello->supportedVersion, &buf[offset], bufLen - offset, &offset);
740         PackHsExtArrayForList(&clientHello->tls13Cookie, &buf[offset], bufLen - offset, &offset);
741         PackHsExtArray8(&clientHello->extendedMasterSecret, &buf[offset], bufLen - offset, &offset);
742         PackHsExtArrayForList(&clientHello->alpn, &buf[offset], bufLen - offset, &offset);
743         PackHsExtArray8(&clientHello->pskModes, &buf[offset], bufLen - offset, &offset);
744         PackHsExtKeyShare(&clientHello->keyshares, &buf[offset], bufLen - offset, &offset);
745         PackHsExtArray8(&clientHello->secRenego, &buf[offset], bufLen - offset, &offset);
746         PackHsExtArrayForTicket(&clientHello->sessionTicket, &buf[offset], bufLen - offset, &offset);
747         PackHsExtArray8(&clientHello->encryptThenMac, &buf[offset], bufLen - offset, &offset);
748         PackHsExtOfferedPsks(&clientHello->psks, &buf[offset], bufLen - offset, &offset);
749         PackHsExtCaList(&clientHello->caList, &buf[offset], bufLen - offset, &offset);
750         if (clientHello->extensionLen.state == INITIAL_FIELD) {
751             uint32_t extensionLen = offset - sizeof(uint16_t) - bufOffset;
752             BSL_Uint16ToByte(extensionLen, &buf[bufOffset]);
753         }
754     }
755 
756 EXIT:
757     *usedLen = offset;
758     return HITLS_SUCCESS;
759 }
760 
PackHsExtUint16(const FRAME_HsExtUint16 * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)761 static int32_t PackHsExtUint16(const FRAME_HsExtUint16 *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
762 {
763     uint32_t repeats = ONE_TIME;
764     // This extension does not need to be assembled.
765     if (field->exState == MISSING_FIELD) {
766         return HITLS_SUCCESS;
767     }
768     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
769     if (field->exState == DUPLICATE_FIELD) {
770         repeats = TWO_TIMES;
771     }
772     // Calculate the total length to be assembled
773     uint32_t length = 0;
774     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
775     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
776     length += field->exLen.data;
777     length *= repeats;
778 
779     // Not enough to assemble
780     if (bufLen < length) {
781         return HITLS_INTERNAL_EXCEPTION;
782     }
783 
784     // Assembly extension type. Duplicate extensions exist. Currently, assembly is performed twice consecutively.
785     uint32_t bufoffset = 0;
786     uint32_t tmpOffset;
787     for (uint32_t i = 0; i < repeats; i++) {
788         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
789         tmpOffset = bufoffset;
790         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
791         PackInteger16(&field->data, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
792         if (field->exLen.state == INITIAL_FIELD) {
793             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
794             BSL_Uint16ToByte(len, &buf[tmpOffset]);
795         }
796     }
797 
798     *offset += bufoffset;
799     return HITLS_SUCCESS;
800 }
801 
PackHsExtServerKeyShare(const FRAME_HsExtServerKeyShare * field,uint8_t * buf,uint32_t bufLen,uint32_t * offset)802 static int32_t PackHsExtServerKeyShare(
803     const FRAME_HsExtServerKeyShare *field, uint8_t *buf, uint32_t bufLen, uint32_t *offset)
804 {
805     uint32_t repeats = ONE_TIME;
806     // This extension does not need to be assembled.
807     if (field->exState == MISSING_FIELD) {
808         return HITLS_SUCCESS;
809     }
810     // Currently, duplicate extension types can be assembled. Only one extension type can be assembled.
811     if (field->exState == DUPLICATE_FIELD) {
812         repeats = TWO_TIMES;
813     }
814     // Calculate the total length to be assembled
815     uint32_t length = 0;
816     length += ((field->exType.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
817     length += ((field->exLen.state == MISSING_FIELD) ? 0 : sizeof(uint16_t));
818     length += field->exLen.data;
819     length *= repeats;
820 
821     // Not enough to assemble
822     if (bufLen < length) {
823         return HITLS_INTERNAL_EXCEPTION;
824     }
825     // Assembly extension type. Duplicate extensions exist. Currently, assembly is performed twice consecutively.
826     uint32_t bufoffset = 0;
827     uint32_t tmpOffset;
828     for (uint32_t i = 0; i < repeats; i++) {
829         PackInteger16(&field->exType, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
830         tmpOffset = bufoffset;
831         PackInteger16(&field->exLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
832         PackInteger16(&field->data.group, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
833         PackInteger16(&field->data.keyExchangeLen, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
834             PackArray8(&field->data.keyExchange, &buf[bufoffset], bufLen - bufoffset, &bufoffset);
835         if (field->exLen.state == INITIAL_FIELD) {
836             uint32_t len = bufoffset - sizeof(uint16_t) - tmpOffset;
837             BSL_Uint16ToByte(len, &buf[tmpOffset]);
838         }
839     }
840     *offset += bufoffset;
841     return HITLS_SUCCESS;
842 }
843 
PackServerHelloMsg(const FRAME_ServerHelloMsg * serverHello,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)844 static int32_t PackServerHelloMsg(const FRAME_ServerHelloMsg *serverHello, uint8_t *buf,
845     uint32_t bufLen, uint32_t *usedLen)
846 {
847     uint32_t offset = 0;
848     uint32_t bufOffset;
849 
850     PackInteger16(&serverHello->version, &buf[offset], bufLen, &offset);
851     PackArray8(&serverHello->randomValue, &buf[offset], bufLen - offset, &offset);
852     PackInteger8(&serverHello->sessionIdSize, &buf[offset], bufLen - offset, &offset);
853     PackArray8(&serverHello->sessionId, &buf[offset], bufLen - offset, &offset);
854     PackInteger16(&serverHello->cipherSuite, &buf[offset], bufLen - offset, &offset);
855     PackInteger8(&serverHello->compressionMethod, &buf[offset], bufLen - offset, &offset);
856 
857     bufOffset = offset;
858     PackInteger16(&serverHello->extensionLen, &buf[offset], bufLen - offset, &offset);
859     PackHsExtArrayForList(&serverHello->serverName, &buf[offset], bufLen - offset, &offset);
860     PackHsExtArrayForList(&serverHello->tls13Cookie, &buf[offset], bufLen - offset, &offset);
861     PackHsExtArrayForTicket(&serverHello->sessionTicket, &buf[offset], bufLen - offset, &offset);
862     PackHsExtUint16(&serverHello->supportedVersion, &buf[offset], bufLen - offset, &offset);
863     PackHsExtArray8(&serverHello->extendedMasterSecret, &buf[offset], bufLen - offset, &offset);
864     PackHsExtArrayForList(&serverHello->alpn, &buf[offset], bufLen - offset, &offset);
865     PackHsExtServerKeyShare(&serverHello->keyShare, &buf[offset], bufLen - offset, &offset);
866     // hello retry request key share
867     PackHsExtArray8(&serverHello->secRenego, &buf[offset], bufLen - offset, &offset);
868     PackHsExtArray8(&serverHello->pointFormats, &buf[offset], bufLen - offset, &offset);
869     PackHsExtUint16(&serverHello->pskSelectedIdentity, &buf[offset], bufLen - offset, &offset);
870     // encrypt then mac
871     PackHsExtArray8(&serverHello->encryptThenMac, &buf[offset], bufLen - offset, &offset);
872 
873 	if (serverHello->extensionLen.state == INITIAL_FIELD) {
874         uint32_t extensionLen = offset - sizeof(uint16_t) - bufOffset;
875         BSL_Uint16ToByte(extensionLen, &buf[bufOffset]);
876     }
877     *usedLen = offset;
878     return HITLS_SUCCESS;
879 }
880 
PackCertificateMsg(FRAME_Type * type,const FRAME_CertificateMsg * certificate,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)881 static int32_t PackCertificateMsg(FRAME_Type *type, const FRAME_CertificateMsg *certificate, uint8_t *buf,
882     uint32_t bufLen, uint32_t *usedLen)
883 {
884     uint32_t offset = 0;
885     uint32_t bufOffset;
886 
887     if (type->versionType == HITLS_VERSION_TLS13) {
888         PackInteger8(&certificate->certificateReqCtxSize, &buf[offset], bufLen - offset, &offset);
889         PackArray8(&certificate->certificateReqCtx, &buf[offset], bufLen - offset, &offset);
890     }
891     bufOffset = offset;
892     PackInteger24(&certificate->certsLen, &buf[offset], bufLen - offset, &offset);
893     const FrameCertItem *next = certificate->certItem;
894     while (next != NULL) {
895         if (next->state == MISSING_FIELD) {
896             break;
897         }
898         PackInteger24(&next->certLen, &buf[offset], bufLen - offset, &offset);
899         PackArray8(&next->cert, &buf[offset], bufLen - offset, &offset);
900         if (type->versionType == HITLS_VERSION_TLS13) {
901             PackInteger16(&next->extensionLen, &buf[offset], bufLen - offset, &offset);
902             PackArray8(&next->extension, &buf[offset], bufLen - offset, &offset);
903         }
904         next = next->next;
905     }
906 
907     if (certificate->certsLen.state == INITIAL_FIELD) {
908         uint32_t certsLen = offset - SIZE_OF_UINT24 - bufOffset;
909         BSL_Uint24ToByte(certsLen, &buf[bufOffset]);
910     }
911 
912     *usedLen = offset;
913     return HITLS_SUCCESS;
914 }
915 
PackServerEcdheMsg(FRAME_Type * type,const FRAME_ServerKeyExchangeMsg * serverKeyExchange,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)916 static int32_t PackServerEcdheMsg(FRAME_Type *type, const FRAME_ServerKeyExchangeMsg *serverKeyExchange, uint8_t *buf,
917     uint32_t bufLen, uint32_t *usedLen)
918 {
919     uint32_t offset = 0;
920 
921     // Fill in the following values in sequence: curve type, curve ID, pubkeylen, pubkey value, signature algorithm,
922     // signature len, and signature value.
923     PackInteger8(&serverKeyExchange->keyEx.ecdh.curveType, &buf[offset], bufLen, &offset);
924     PackInteger16(&serverKeyExchange->keyEx.ecdh.namedcurve, &buf[offset], bufLen - offset, &offset);
925     PackInteger8(&serverKeyExchange->keyEx.ecdh.pubKeySize, &buf[offset], bufLen- offset, &offset);
926     PackArray8(&serverKeyExchange->keyEx.ecdh.pubKey, &buf[offset], bufLen- offset, &offset);
927     if (((IS_DTLS_VERSION(type->versionType)) && (type->versionType <= HITLS_VERSION_DTLS12)) ||
928         ((!IS_DTLS_VERSION(type->versionType)) && (type->versionType >= HITLS_VERSION_TLS12))) {
929         // DTLS1.2, TLS1.2, and later versions
930         PackInteger16(&serverKeyExchange->keyEx.ecdh.signAlgorithm, &buf[offset], bufLen- offset, &offset);
931     }
932 
933     PackInteger16(&serverKeyExchange->keyEx.ecdh.signSize, &buf[offset], bufLen- offset, &offset);
934     PackArray8(&serverKeyExchange->keyEx.ecdh.signData, &buf[offset], bufLen- offset, &offset);
935 
936     *usedLen = offset;
937     return HITLS_SUCCESS;
938 }
939 
PackServerDheMsg(FRAME_Type * type,const FRAME_ServerKeyExchangeMsg * serverKeyExchange,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)940 static int32_t PackServerDheMsg(FRAME_Type *type, const FRAME_ServerKeyExchangeMsg *serverKeyExchange, uint8_t *buf,
941     uint32_t bufLen, uint32_t *usedLen)
942 {
943     uint32_t offset = 0;
944 
945     // Fill in the following values in sequence: plen, p value, glen, g value, pubkeylen, pubkey value,
946     // signature algorithm, signature len, and signature value.
947     PackInteger16(&serverKeyExchange->keyEx.dh.plen, &buf[offset], bufLen, &offset);
948     PackArray8(&serverKeyExchange->keyEx.dh.p, &buf[offset], bufLen - offset, &offset);
949     PackInteger16(&serverKeyExchange->keyEx.dh.glen, &buf[offset], bufLen - offset, &offset);
950     PackArray8(&serverKeyExchange->keyEx.dh.g, &buf[offset], bufLen - offset, &offset);
951     PackInteger16(&serverKeyExchange->keyEx.dh.pubKeyLen, &buf[offset], bufLen- offset, &offset);
952     PackArray8(&serverKeyExchange->keyEx.dh.pubKey, &buf[offset], bufLen- offset, &offset);
953     if (((IS_DTLS_VERSION(type->versionType)) && (type->versionType <= HITLS_VERSION_DTLS12)) ||
954         ((!IS_DTLS_VERSION(type->versionType)) && (type->versionType >= HITLS_VERSION_TLS12))) {
955         // DTLS1.2, TLS1.2, and later versions
956         PackInteger16(&serverKeyExchange->keyEx.dh.signAlgorithm, &buf[offset], bufLen- offset, &offset);
957     }
958     PackInteger16(&serverKeyExchange->keyEx.dh.signSize, &buf[offset], bufLen- offset, &offset);
959     PackArray8(&serverKeyExchange->keyEx.dh.signData, &buf[offset], bufLen- offset, &offset);
960 
961     *usedLen = offset;
962     return HITLS_SUCCESS;
963 }
964 
PackServerEccMsg(const FRAME_ServerKeyExchangeMsg * serverKeyExchange,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)965 static int32_t PackServerEccMsg(const FRAME_ServerKeyExchangeMsg *serverKeyExchange, uint8_t *buf,
966     uint32_t bufLen, uint32_t *usedLen)
967 {
968     uint32_t offset = 0;
969     PackInteger16(&serverKeyExchange->keyEx.ecdh.signSize, &buf[offset], bufLen- offset, &offset);
970     PackArray8(&serverKeyExchange->keyEx.ecdh.signData, &buf[offset], bufLen- offset, &offset);
971 
972     *usedLen = offset;
973     return HITLS_SUCCESS;
974 }
975 
PackServerKeyExchangeMsg(FRAME_Type * type,const FRAME_ServerKeyExchangeMsg * serverKeyExchange,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)976 static int32_t PackServerKeyExchangeMsg(FRAME_Type *type, const FRAME_ServerKeyExchangeMsg *serverKeyExchange,
977     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
978 {
979     // Currently, ECDHE and DHE key exchange packets can be assembled.
980     if (type->keyExType == HITLS_KEY_EXCH_ECDHE) {
981         return PackServerEcdheMsg(type, serverKeyExchange, buf, bufLen, usedLen);
982     } else if (type->keyExType == HITLS_KEY_EXCH_DHE) {
983         return PackServerDheMsg(type, serverKeyExchange, buf, bufLen, usedLen);
984     } else if (type->keyExType == HITLS_KEY_EXCH_ECC) {
985         return PackServerEccMsg(serverKeyExchange, buf, bufLen, usedLen);
986     }
987 
988     return HITLS_PACK_UNSUPPORT_KX_ALG;
989 }
990 
PackCertificateRequestExt(uint32_t type,const FRAME_CertificateRequestMsg * certificateRequest,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)991 static int32_t PackCertificateRequestExt(uint32_t type, const FRAME_CertificateRequestMsg *certificateRequest,
992     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
993 {
994     uint32_t offset = 0;
995     FRAME_Integer exType;
996     FRAME_Integer size;
997     switch (type) {
998         case HS_EX_TYPE_SIGNATURE_ALGORITHMS:
999             exType.data = HS_EX_TYPE_SIGNATURE_ALGORITHMS;
1000             exType.state = INITIAL_FIELD;
1001             PackInteger16(&exType, &buf[offset], bufLen, &offset);
1002             size.data = certificateRequest->signatureAlgorithmsSize.data + sizeof(uint16_t);
1003             size.state = INITIAL_FIELD;
1004             PackInteger16(&size, &buf[offset], bufLen, &offset);
1005 
1006             PackInteger16(&certificateRequest->signatureAlgorithmsSize, &buf[offset], bufLen, &offset);
1007             PackArray16(&certificateRequest->signatureAlgorithms, &buf[offset], bufLen - offset, &offset);
1008 
1009             break;
1010         default:
1011             break;
1012     }
1013 
1014     *usedLen += offset;
1015     return HITLS_SUCCESS;
1016 }
1017 
PackCertificateRequestMsg(FRAME_Type * type,const FRAME_CertificateRequestMsg * certificateRequest,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1018 static int32_t PackCertificateRequestMsg(FRAME_Type *type, const FRAME_CertificateRequestMsg *certificateRequest,
1019     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1020 {
1021     uint32_t offset = 0;
1022     if (certificateRequest->state == MISSING_FIELD){
1023         return HITLS_SUCCESS;
1024     }
1025     if (type->versionType != HITLS_VERSION_TLS13) {
1026         PackInteger8(&certificateRequest->certTypesSize, &buf[offset], bufLen, &offset);
1027         PackArray8(&certificateRequest->certTypes, &buf[offset], bufLen - offset, &offset);
1028         PackInteger16(&certificateRequest->signatureAlgorithmsSize, &buf[offset], bufLen, &offset);
1029         PackArray16(&certificateRequest->signatureAlgorithms, &buf[offset], bufLen - offset, &offset);
1030         PackInteger16(&certificateRequest->distinguishedNamesSize, &buf[offset], bufLen, &offset);
1031         PackArray8(&certificateRequest->distinguishedNames, &buf[offset], bufLen - offset, &offset);
1032     } else {
1033         PackInteger8(&certificateRequest->certificateReqCtxSize, &buf[offset], bufLen, &offset);
1034         PackArray8(&certificateRequest->certificateReqCtx, &buf[offset], bufLen - offset, &offset);
1035         // Packaged extension
1036         uint32_t tmpOffset = offset;
1037         PackInteger16(&certificateRequest->exMsgLen, &buf[offset], bufLen, &offset);
1038 
1039         bool ifPackSign = (certificateRequest->signatureAlgorithmsSize.state != MISSING_FIELD);
1040 
1041         // Package HS_EX_TYPE_SIGNATURE_ALGORITHMS Extensions
1042         if(ifPackSign) {
1043             PackCertificateRequestExt(HS_EX_TYPE_SIGNATURE_ALGORITHMS, certificateRequest, &buf[offset],
1044                                         bufLen - offset, &offset);
1045         }
1046         if(certificateRequest->signatureAlgorithmsSize.state == DUPLICATE_FIELD) {
1047             PackCertificateRequestExt(HS_EX_TYPE_SIGNATURE_ALGORITHMS, certificateRequest, &buf[offset],
1048                                         bufLen - offset, &offset);
1049         }
1050         if (certificateRequest->exMsgLen.state == INITIAL_FIELD) {
1051             uint32_t len = offset - sizeof(uint16_t) - tmpOffset;
1052             BSL_Uint16ToByte(len, &buf[tmpOffset]);
1053         }
1054     }
1055 
1056     *usedLen = offset;
1057     return HITLS_SUCCESS;
1058 }
1059 
PackServerHelloDoneMsg(const FRAME_ServerHelloDoneMsg * serverHelloDone,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1060 static int32_t PackServerHelloDoneMsg(const FRAME_ServerHelloDoneMsg *serverHelloDone, uint8_t *buf,
1061     uint32_t bufLen, uint32_t *usedLen)
1062 {
1063     uint32_t offset = 0;
1064 
1065     /* The ServerHelloDone packet is an empty packet. Extra data is assembled here to construct abnormal packets. */
1066     PackArray8(&serverHelloDone->extra, &buf[offset], bufLen, &offset);
1067 
1068     *usedLen = offset;
1069     return HITLS_SUCCESS;
1070 }
1071 
PackClientEcdheMsg(FRAME_Type * type,const FRAME_ClientKeyExchangeMsg * clientKeyExchange,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1072 static int32_t PackClientEcdheMsg(FRAME_Type *type, const FRAME_ClientKeyExchangeMsg *clientKeyExchange, uint8_t *buf,
1073     uint32_t bufLen, uint32_t *usedLen)
1074 {
1075     uint32_t offset = 0;
1076     if (type->versionType == HITLS_VERSION_TLCP_DTLCP11) { /* Three bytes are added to the client key exchange. */
1077         buf[offset] = HITLS_EC_CURVE_TYPE_NAMED_CURVE;
1078         offset += sizeof(uint8_t);
1079         BSL_Uint16ToByte(HITLS_EC_GROUP_SM2, &buf[offset]);
1080         offset += sizeof(uint16_t);
1081     }
1082     PackInteger8(&clientKeyExchange->pubKeySize, &buf[offset], bufLen, &offset);
1083     PackArray8(&clientKeyExchange->pubKey, &buf[offset], bufLen - offset, &offset);
1084 
1085     *usedLen = offset;
1086     return HITLS_SUCCESS;
1087 }
1088 
PackClientDheMsg(const FRAME_ClientKeyExchangeMsg * clientKeyExchange,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1089 static int32_t PackClientDheMsg(const FRAME_ClientKeyExchangeMsg *clientKeyExchange, uint8_t *buf,
1090     uint32_t bufLen, uint32_t *usedLen)
1091 {
1092     uint32_t offset = 0;
1093 
1094     PackInteger16(&clientKeyExchange->pubKeySize, &buf[offset], bufLen, &offset);
1095     PackArray8(&clientKeyExchange->pubKey, &buf[offset], bufLen - offset, &offset);
1096 
1097     *usedLen = offset;
1098     return HITLS_SUCCESS;
1099 }
1100 
PackClientKeyExchangeMsg(FRAME_Type * type,const FRAME_ClientKeyExchangeMsg * clientKeyExchange,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1101 static int32_t PackClientKeyExchangeMsg(FRAME_Type *type, const FRAME_ClientKeyExchangeMsg *clientKeyExchange,
1102     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1103 {
1104     // Currently, ECDHE and DHE key exchange packets can be assembled.
1105     if (type->keyExType == HITLS_KEY_EXCH_ECDHE) {
1106         return PackClientEcdheMsg(type, clientKeyExchange, buf, bufLen, usedLen);
1107     } else if (type->keyExType == HITLS_KEY_EXCH_DHE || type->keyExType == HITLS_KEY_EXCH_RSA) {
1108         return PackClientDheMsg(clientKeyExchange, buf, bufLen, usedLen);
1109     }
1110 
1111     return HITLS_PACK_UNSUPPORT_KX_ALG;
1112 }
1113 
PackCertificateVerifyMsg(FRAME_Type * type,const FRAME_CertificateVerifyMsg * certificateVerify,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1114 static int32_t PackCertificateVerifyMsg(FRAME_Type *type, const FRAME_CertificateVerifyMsg *certificateVerify,
1115     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1116 {
1117     uint32_t offset = 0;
1118 
1119     if (((IS_DTLS_VERSION(type->versionType)) && (type->versionType <= HITLS_VERSION_DTLS12)) ||
1120         ((!IS_DTLS_VERSION(type->versionType)) && (type->versionType >= HITLS_VERSION_TLS12))) {
1121         // DTLS1.2, TLS1.2, and later versions
1122         PackInteger16(&certificateVerify->signHashAlg, &buf[offset], bufLen, &offset);
1123     }
1124     PackInteger16(&certificateVerify->signSize, &buf[offset], bufLen - offset, &offset);
1125     PackArray8(&certificateVerify->sign, &buf[offset], bufLen - offset, &offset);
1126 
1127     *usedLen = offset;
1128     return HITLS_SUCCESS;
1129 }
1130 
PackFinishedMsg(const FRAME_FinishedMsg * finished,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1131 static int32_t PackFinishedMsg(const FRAME_FinishedMsg *finished, uint8_t *buf,
1132     uint32_t bufLen, uint32_t *usedLen)
1133 {
1134     uint32_t offset = 0;
1135 
1136     PackArray8(&finished->verifyData, &buf[offset], bufLen - offset, &offset);
1137 
1138     *usedLen = offset;
1139     return HITLS_SUCCESS;
1140 }
1141 
PackHsMsgHeader(uint16_t version,const FRAME_HsMsg * hsMsg,uint32_t bodyLen,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen,BSL_UIO_TransportType transportType)1142 static void PackHsMsgHeader(uint16_t version, const FRAME_HsMsg *hsMsg, uint32_t bodyLen,
1143     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen, BSL_UIO_TransportType transportType)
1144 {
1145     (void)version;
1146     uint32_t offset = 0;
1147     uint32_t bufOffset;
1148 
1149     PackInteger8(&hsMsg->type, &buf[offset], bufLen, &offset);
1150 
1151     bufOffset = offset;
1152     PackInteger24(&hsMsg->length, &buf[offset], bufLen - offset, &offset);
1153     if (IS_TRANSTYPE_DATAGRAM(transportType)) {
1154         PackInteger16(&hsMsg->sequence, &buf[offset], bufLen - offset, &offset);
1155         PackInteger24(&hsMsg->fragmentOffset, &buf[offset], bufLen - offset, &offset);
1156         if (hsMsg->fragmentLength.state == INITIAL_FIELD) {
1157             BSL_Uint24ToByte(bodyLen, &buf[offset]);
1158             offset += SIZE_OF_UINT24;
1159         } else {
1160             PackInteger24(&hsMsg->fragmentLength, &buf[offset], bufLen - offset, &offset);
1161         }
1162     }
1163 
1164     if (hsMsg->length.state == INITIAL_FIELD) {
1165         BSL_Uint24ToByte(bodyLen, &buf[bufOffset]);
1166     }
1167 
1168     *usedLen = offset;
1169 }
1170 
PackNewSessionTicketMsg(FRAME_Type * type,const FRAME_NewSessionTicketMsg * newSessionTicket,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1171 static int32_t PackNewSessionTicketMsg(FRAME_Type *type, const FRAME_NewSessionTicketMsg *newSessionTicket,
1172     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1173 {
1174     uint32_t offset = 0;
1175     PackInteger32(&newSessionTicket->ticketLifetime, &buf[offset], bufLen - offset, &offset);
1176     if (type->versionType != HITLS_VERSION_TLS13) {
1177         PackInteger16(&newSessionTicket->ticketSize, &buf[offset], bufLen - offset, &offset);
1178         PackArray8(&newSessionTicket->ticket, &buf[offset], bufLen - offset, &offset);
1179     } else {
1180         PackInteger32(&newSessionTicket->ticketAgeAdd, &buf[offset], bufLen - offset, &offset);
1181         PackInteger8(&newSessionTicket->ticketNonceSize, &buf[offset], bufLen - offset, &offset);
1182         PackArray8(&newSessionTicket->ticketNonce, &buf[offset], bufLen - offset, &offset);
1183         PackInteger16(&newSessionTicket->ticketSize, &buf[offset], bufLen - offset, &offset);
1184         PackArray8(&newSessionTicket->ticket, &buf[offset], bufLen - offset, &offset);
1185         PackInteger16(&newSessionTicket->extensionLen, &buf[offset], bufLen - offset, &offset);
1186     }
1187     *usedLen = offset;
1188     if (offset != bufLen) {
1189         return HITLS_PACK_UNSUPPORT_HANDSHAKE_MSG;
1190     }
1191     return HITLS_SUCCESS;
1192 }
1193 
PackHsMsgBody(FRAME_Type * type,const FRAME_Msg * msg,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1194 static int32_t PackHsMsgBody(FRAME_Type *type, const FRAME_Msg *msg,
1195     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1196 {
1197     int32_t ret;
1198 
1199     const FRAME_HsMsg *hsMsg = &(msg->body.hsMsg);
1200 
1201     switch (type->handshakeType) {
1202         case CLIENT_HELLO:
1203             ret = PackClientHelloMsg(&(hsMsg->body.clientHello), buf, bufLen, usedLen);
1204             break;
1205         case SERVER_HELLO:
1206             ret = PackServerHelloMsg(&(hsMsg->body.serverHello), buf, bufLen, usedLen);
1207             break;
1208         case CERTIFICATE:
1209             ret = PackCertificateMsg(type, &(hsMsg->body.certificate), buf, bufLen, usedLen);
1210             break;
1211         case SERVER_KEY_EXCHANGE:
1212             ret = PackServerKeyExchangeMsg(type, &(hsMsg->body.serverKeyExchange), buf, bufLen, usedLen);
1213             break;
1214         case CERTIFICATE_REQUEST:
1215             ret = PackCertificateRequestMsg(type, &(hsMsg->body.certificateReq), buf, bufLen, usedLen);
1216             break;
1217         case SERVER_HELLO_DONE:
1218             ret = PackServerHelloDoneMsg(&(hsMsg->body.serverHelloDone), buf, bufLen, usedLen);
1219             break;
1220         case CLIENT_KEY_EXCHANGE:
1221             ret = PackClientKeyExchangeMsg(type, &(hsMsg->body.clientKeyExchange), buf, bufLen, usedLen);
1222             break;
1223         case CERTIFICATE_VERIFY:
1224             ret = PackCertificateVerifyMsg(type, &(hsMsg->body.certificateVerify), buf, bufLen, usedLen);
1225             break;
1226         case FINISHED:
1227             ret = PackFinishedMsg(&(hsMsg->body.finished), buf, bufLen, usedLen);
1228             break;
1229         case NEW_SESSION_TICKET:
1230             ret = PackNewSessionTicketMsg(type, &(hsMsg->body.newSessionTicket), buf, bufLen, usedLen);
1231             break;
1232         default:
1233             ret = HITLS_PACK_UNSUPPORT_HANDSHAKE_MSG;
1234             break;
1235     }
1236 
1237     return ret;
1238 }
1239 
PackHandShakeMsg(FRAME_Type * type,const FRAME_Msg * msg,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1240 static int32_t PackHandShakeMsg(FRAME_Type *type, const FRAME_Msg *msg,
1241     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1242 {
1243     const FRAME_HsMsg *hsMsg = &(msg->body.hsMsg);
1244     uint32_t ret;
1245     uint32_t offset;
1246     uint32_t bodyMaxLen;
1247     uint32_t headerLen;
1248     uint32_t bodyLen = 0;
1249 
1250     if (IS_TRANSTYPE_DATAGRAM(type->transportType)) { // DTLS
1251         if (bufLen < DTLS_HS_MSG_HEADER_SIZE) {
1252             return HITLS_INTERNAL_EXCEPTION;
1253         }
1254 
1255         bodyMaxLen = bufLen - DTLS_HS_MSG_HEADER_SIZE;
1256         offset = DTLS_HS_MSG_HEADER_SIZE;
1257         headerLen = DTLS_HS_MSG_HEADER_SIZE;
1258     } else {                                      // TLS
1259         if (bufLen < HS_MSG_HEADER_SIZE) {
1260             return HITLS_INTERNAL_EXCEPTION;
1261         }
1262 
1263         bodyMaxLen = bufLen - HS_MSG_HEADER_SIZE;
1264         offset = HS_MSG_HEADER_SIZE;
1265         headerLen = HS_MSG_HEADER_SIZE;
1266     }
1267 
1268     // Assemble the body of the handshake message.
1269     ret = PackHsMsgBody(type, msg, &buf[offset], bodyMaxLen, &bodyLen);
1270     if (ret != HITLS_SUCCESS) {
1271         return ret;
1272     }
1273 
1274     // Assemble the handshake packet header.
1275     PackHsMsgHeader(type->versionType, hsMsg, bodyLen, buf, headerLen, &headerLen, type->transportType);
1276 
1277     // Splicing body and head
1278     // If some fields are missing in the header, the packet body is filled with an offset forward.
1279     if (headerLen != offset) {
1280         ret = memmove_s(&buf[headerLen], bufLen - headerLen, &buf[offset], bodyLen);
1281         if (ret != EOK) {
1282             return ret;
1283         }
1284     }
1285     *usedLen = headerLen + bodyLen;
1286     return ret;
1287 }
1288 
PackCcsMsg(const FRAME_Msg * msg,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1289 static int32_t PackCcsMsg(const FRAME_Msg *msg, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1290 {
1291     uint32_t offset = 0;
1292 
1293     PackInteger8(&msg->body.ccsMsg.ccsType, &buf[offset], bufLen, &offset);
1294     /* Extra data is used to construct abnormal packets. */
1295     PackArray8(&msg->body.ccsMsg.extra, &buf[offset], bufLen - offset, &offset);
1296 
1297     *usedLen = offset;
1298     return HITLS_SUCCESS;
1299 }
1300 
PackAlertMsg(const FRAME_Msg * msg,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1301 static int32_t PackAlertMsg(const FRAME_Msg *msg, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1302 {
1303     uint32_t offset = 0;
1304 
1305     PackInteger8(&msg->body.alertMsg.alertLevel, &buf[offset], bufLen, &offset);
1306     PackInteger8(&msg->body.alertMsg.alertDescription, &buf[offset], bufLen - offset, &offset);
1307     /* Extra data is used to construct abnormal packets. */
1308     PackArray8(&msg->body.alertMsg.extra, &buf[offset], bufLen - offset, &offset);
1309 
1310     *usedLen = offset;
1311     return HITLS_SUCCESS;
1312 }
1313 
PackAppMsg(const FRAME_Msg * msg,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)1314 static int32_t PackAppMsg(const FRAME_Msg *msg, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
1315 {
1316     uint32_t offset = 0;
1317 
1318     PackArray8(&msg->body.appMsg.appData, &buf[offset], bufLen, &offset);
1319 
1320     *usedLen = offset;
1321     return HITLS_SUCCESS;
1322 }
1323 
PackRecordHeader(uint16_t version,const FRAME_Msg * msg,uint32_t bodyLen,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen,BSL_UIO_TransportType transportType)1324 static int32_t PackRecordHeader(uint16_t version, const FRAME_Msg *msg, uint32_t bodyLen,
1325     uint8_t *buf, uint32_t bufLen, uint32_t *usedLen, BSL_UIO_TransportType transportType)
1326 {
1327     (void)version;
1328     uint32_t offset = 0;
1329 
1330     PackInteger8(&msg->recType, &buf[offset], bufLen, &offset);
1331     PackInteger16(&msg->recVersion, &buf[offset], bufLen - offset, &offset);
1332     if (IS_TRANSTYPE_DATAGRAM(transportType)) {
1333         PackInteger16(&msg->epoch, &buf[offset], bufLen - offset, &offset);
1334         PackInteger48(&msg->sequence, &buf[offset], bufLen - offset, &offset);
1335     }
1336 
1337     if (msg->length.state == INITIAL_FIELD) {
1338         BSL_Uint16ToByte(bodyLen, &buf[offset]);
1339         offset += sizeof(uint16_t);
1340     } else {
1341         PackInteger16(&msg->length, &buf[offset], bufLen - offset, &offset);
1342     }
1343 
1344     *usedLen = offset;
1345     return HITLS_SUCCESS;
1346 }
1347 
FRAME_PackRecordBody(FRAME_Type * frameType,const FRAME_Msg * msg,uint8_t * buffer,uint32_t bufLen,uint32_t * usedLen)1348 int32_t FRAME_PackRecordBody(FRAME_Type *frameType, const FRAME_Msg *msg,
1349     uint8_t *buffer, uint32_t bufLen, uint32_t *usedLen)
1350 {
1351     int32_t ret;
1352 
1353     // pack Body
1354     switch (frameType->recordType) {
1355         case REC_TYPE_HANDSHAKE:
1356             ret = PackHandShakeMsg(frameType, msg, buffer, bufLen, usedLen);
1357             break;
1358         case REC_TYPE_CHANGE_CIPHER_SPEC:
1359             ret = PackCcsMsg(msg, buffer, bufLen, usedLen);
1360             break;
1361         case REC_TYPE_ALERT:
1362             ret = PackAlertMsg(msg, buffer, bufLen, usedLen);
1363             break;
1364         case REC_TYPE_APP:
1365             ret = PackAppMsg(msg, buffer, bufLen, usedLen);
1366             break;
1367         default:
1368             ret = HITLS_INTERNAL_EXCEPTION;
1369             break;
1370     }
1371 
1372     return ret;
1373 }
1374 
FRAME_PackMsg(FRAME_Type * frameType,const FRAME_Msg * msg,uint8_t * buffer,uint32_t bufLen,uint32_t * usedLen)1375 int32_t FRAME_PackMsg(FRAME_Type *frameType, const FRAME_Msg *msg, uint8_t *buffer, uint32_t bufLen, uint32_t *usedLen)
1376 {
1377     int32_t ret;
1378     uint32_t offset;
1379     uint32_t bodyMaxLen;
1380     uint32_t headerLen;
1381     uint32_t bodyLen = 0;
1382 
1383     if (msg == NULL || buffer == NULL || usedLen == NULL) {
1384         return HITLS_INTERNAL_EXCEPTION;
1385     }
1386 
1387     if (IS_TRANSTYPE_DATAGRAM(frameType->transportType)) { // DTLS
1388         if (bufLen < DTLS_RECORD_HEADER_LEN) {
1389             return HITLS_INTERNAL_EXCEPTION;
1390         }
1391 
1392         bodyMaxLen = bufLen - DTLS_RECORD_HEADER_LEN;
1393         offset = DTLS_RECORD_HEADER_LEN;
1394         headerLen = DTLS_RECORD_HEADER_LEN;
1395     } else {                                      // TLS
1396         if (bufLen < TLS_RECORD_HEADER_LEN) {
1397             return HITLS_INTERNAL_EXCEPTION;
1398         }
1399 
1400         bodyMaxLen = bufLen - TLS_RECORD_HEADER_LEN;
1401         offset = TLS_RECORD_HEADER_LEN;
1402         headerLen = TLS_RECORD_HEADER_LEN;
1403     }
1404 
1405     // Assemble the message body.
1406     ret = FRAME_PackRecordBody(frameType, msg, &buffer[offset], bodyMaxLen, &bodyLen);
1407     if (ret != HITLS_SUCCESS) {
1408         return ret;
1409     }
1410 
1411     // Assemble the packet header.
1412     PackRecordHeader(frameType->versionType, msg, bodyLen, buffer, headerLen, &headerLen, frameType->transportType);
1413 
1414     // Splicing body and head
1415     // If some fields are missing in the header, the packet body is filled with an offset forward.
1416     if (headerLen != offset) {
1417         ret = memmove_s(&buffer[headerLen], bufLen - headerLen, &buffer[offset], bodyLen);
1418         if (ret != EOK) {
1419             return ret;
1420         }
1421     }
1422     *usedLen = headerLen + bodyLen;
1423     return ret;
1424 }
1425 
FRAME_GetTls13DisorderHsMsg(HS_MsgType type,uint8_t * buffer,uint32_t bufLen,uint32_t * usedLen)1426 int32_t FRAME_GetTls13DisorderHsMsg(HS_MsgType type, uint8_t *buffer, uint32_t bufLen, uint32_t *usedLen)
1427 {
1428     if (bufLen < 5) {
1429         return HITLS_INTERNAL_EXCEPTION;
1430     }
1431     buffer[0] = type;
1432     buffer[1] = 0;
1433     buffer[2] = 0;
1434     buffer[3] = 1;
1435     buffer[4] = 0;
1436     *usedLen = 5;
1437     return HITLS_SUCCESS;
1438 }