• 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 #include <stdint.h>
16 #include "hitls_build.h"
17 #include "securec.h"
18 #include "bsl_err_internal.h"
19 #include "tls_binlog_id.h"
20 #include "bsl_log_internal.h"
21 #include "bsl_log.h"
22 #include "bsl_bytes.h"
23 #include "hitls_error.h"
24 #include "hs_msg.h"
25 #include "pack_common.h"
26 
27 #ifdef HITLS_TLS_PROTO_DTLS12
28 /**
29  * @brief Pack the packet header.
30  *
31  * @param type [IN] message type
32  * @param sequence [IN] Sequence number (dedicated for DTLS)
33  * @param length [IN] message body length
34  * @param buf [OUT] message header
35  */
PackDtlsMsgHeader(HS_MsgType type,uint16_t sequence,uint32_t length,uint8_t * buf)36 void PackDtlsMsgHeader(HS_MsgType type, uint16_t sequence, uint32_t length, uint8_t *buf)
37 {
38     buf[0] = (uint8_t)type & 0xffu;                               /** Type of the handshake message */
39     BSL_Uint24ToByte(length, &buf[DTLS_HS_MSGLEN_ADDR]); /** Fills the length of the handshake message */
40     BSL_Uint16ToByte(
41         sequence, &buf[DTLS_HS_MSGSEQ_ADDR]); /** The 2 bytes starting from the 4th byte are the sn of the message */
42     BSL_Uint24ToByte(
43         0, &buf[DTLS_HS_FRAGMENT_OFFSET_ADDR]); /** The 3 bytes starting from the 6th byte are the fragment offset. */
44     BSL_Uint24ToByte(
45         length, &buf[DTLS_HS_FRAGMENT_LEN_ADDR]); /** The 3 bytes starting from the 9th byte are the fragment length. */
46 }
47 #endif /* HITLS_TLS_PROTO_DTLS12 */
48 
49 #if defined(HITLS_TLS_FEATURE_SESSION_ID) || defined(HITLS_TLS_PROTO_TLS13)
50 /**
51  * @brief Pack the message session ID.
52  *
53  * @param id [IN] Session ID
54  * @param idSize [IN] Session ID length
55  * @param buf [OUT] message buffer
56  * @param bufLen [IN] Maximum message length
57  * @param usedLen [OUT] Length of the packed message
58  *
59  * @retval HITLS_SUCCESS Assembly succeeded.
60  * @retval HITLS_PACK_SESSIONID_ERR Failed to pack the sessionId.
61  * @retval HITLS_MEMCPY_FAIL Memory Copy Failure
62  */
PackSessionId(const uint8_t * id,uint32_t idSize,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)63 int32_t PackSessionId(const uint8_t *id, uint32_t idSize, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
64 {
65     /* If the sessionId length does not meet the requirement, an error code is returned */
66     if ((idSize != 0) && ((idSize > TLS_HS_MAX_SESSION_ID_SIZE) || (idSize < TLS_HS_MIN_SESSION_ID_SIZE))) {
67         BSL_ERR_PUSH_ERROR(HITLS_PACK_SESSIONID_ERR);
68         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15849, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
69             "session id size is incorrect when pace session id.", 0, 0, 0, 0);
70         return HITLS_PACK_SESSIONID_ERR;
71     }
72 
73     uint32_t bufOffset = 0u;
74     buf[bufOffset] = (uint8_t)idSize;
75 
76     /* Calculate the buffer offset length */
77     bufOffset += sizeof(uint8_t);
78     /* If the value of sessionId is 0, a success message is returned */
79     if (idSize == 0u) {
80         *usedLen = bufOffset;
81         return HITLS_SUCCESS;
82     }
83 
84     if ((bufLen - bufOffset) < idSize) {
85         return PackBufLenError(BINLOG_ID15850, BINGLOG_STR("session id"));
86     }
87     /* Copy the session ID */
88     (void)memcpy_s(&buf[bufOffset], bufLen - bufOffset, id, idSize);
89     /* Update the offset length */
90     bufOffset += idSize;
91 
92     *usedLen = bufOffset;
93     return HITLS_SUCCESS;
94 }
95 #endif /* #if HITLS_TLS_FEATURE_SESSION_ID || HITLS_TLS_PROTO_TLS13 */
96 
PackBufLenError(uint32_t logId,const void * format)97 int32_t PackBufLenError(uint32_t logId, const void *format)
98 {
99     BSL_ERR_PUSH_ERROR(HITLS_PACK_NOT_ENOUGH_BUF_LENGTH);
100     if (format != NULL) {
101         BSL_LOG_BINLOG_VARLEN(logId, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "buffer not enough when pack %s.",
102             format);
103     }
104     return HITLS_PACK_NOT_ENOUGH_BUF_LENGTH;
105 }