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 <stdlib.h>
17 #include <stdint.h>
18 #include "hitls_build.h"
19 #include "tls_binlog_id.h"
20 #include "bsl_log_internal.h"
21 #include "bsl_log.h"
22 #include "bsl_err_internal.h"
23 #include "bsl_bytes.h"
24 #include "hitls_error.h"
25 #include "hitls_config.h"
26 #include "tls.h"
27 #include "hs_msg.h"
28 #include "hs_ctx.h"
29 #include "hs.h"
30 #include "pack_msg.h"
31 #include "pack_common.h"
32
33 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
PackHsMsgBody(TLS_Ctx * ctx,HS_MsgType type,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)34 static int32_t PackHsMsgBody(TLS_Ctx *ctx, HS_MsgType type, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
35 {
36 int32_t ret = HITLS_SUCCESS;
37 switch (type) {
38 #ifdef HITLS_TLS_HOST_SERVER
39 case SERVER_HELLO:
40 ret = PackServerHello(ctx, buf, bufLen, usedLen);
41 break;
42 #if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
43 case HELLO_VERIFY_REQUEST:
44 ret = PackHelloVerifyRequest(ctx, buf, bufLen, usedLen);
45 break;
46 #endif
47 case SERVER_KEY_EXCHANGE:
48 ret = PackServerKeyExchange(ctx, buf, bufLen, usedLen);
49 break;
50 case CERTIFICATE_REQUEST:
51 ret = PackCertificateRequest(ctx, buf, bufLen, usedLen);
52 break;
53 case HELLO_REQUEST:
54 case SERVER_HELLO_DONE:
55 return HITLS_SUCCESS;
56 #ifdef HITLS_TLS_FEATURE_SESSION_TICKET
57 case NEW_SESSION_TICKET:
58 ret = PackNewSessionTicket(ctx, buf, bufLen, usedLen);
59 break;
60 #endif /* HITLS_TLS_FEATURE_SESSION_TICKET */
61 #endif /* HITLS_TLS_HOST_SERVER */
62 #ifdef HITLS_TLS_HOST_CLIENT
63 case CLIENT_HELLO:
64 ret = PackClientHello(ctx, buf, bufLen, usedLen);
65 break;
66 case CLIENT_KEY_EXCHANGE:
67 ret = PackClientKeyExchange(ctx, buf, bufLen, usedLen);
68 break;
69 case CERTIFICATE_VERIFY:
70 ret = PackCertificateVerify(ctx, buf, bufLen, usedLen);
71 break;
72 #endif /* HITLS_TLS_HOST_CLIENT */
73 case CERTIFICATE:
74 ret = PackCertificate(ctx, buf, bufLen, usedLen);
75 break;
76 case FINISHED:
77 ret = PackFinished(ctx, buf, bufLen, usedLen);
78 break;
79 default:
80 ret = HITLS_PACK_UNSUPPORT_HANDSHAKE_MSG;
81 break;
82 }
83
84 if (ret != HITLS_SUCCESS) {
85 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15812, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
86 "pack handshake[%u] msg error.", type, 0, 0, 0);
87 }
88 return ret;
89 }
90 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
91 #ifdef HITLS_TLS_PROTO_TLS13
PackTls13HsMsgBody(TLS_Ctx * ctx,HS_MsgType type,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)92 static int32_t PackTls13HsMsgBody(TLS_Ctx *ctx, HS_MsgType type, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
93 {
94 int32_t ret = HITLS_SUCCESS;
95 switch (type) {
96 #ifdef HITLS_TLS_HOST_CLIENT
97 case CLIENT_HELLO:
98 ret = PackClientHello(ctx, buf, bufLen, usedLen);
99 break;
100 #endif /* HITLS_TLS_HOST_CLIENT */
101 #ifdef HITLS_TLS_HOST_SERVER
102 case SERVER_HELLO:
103 ret = PackServerHello(ctx, buf, bufLen, usedLen);
104 break;
105 case ENCRYPTED_EXTENSIONS:
106 ret = PackEncryptedExtensions(ctx, buf, bufLen, usedLen);
107 break;
108 case CERTIFICATE_REQUEST:
109 ret = Tls13PackCertificateRequest(ctx, buf, bufLen, usedLen);
110 break;
111 case NEW_SESSION_TICKET:
112 ret = Tls13PackNewSessionTicket(ctx, buf, bufLen, usedLen);
113 break;
114 #endif /* HITLS_TLS_HOST_SERVER */
115 case CERTIFICATE:
116 ret = Tls13PackCertificate(ctx, buf, bufLen, usedLen);
117 break;
118 case CERTIFICATE_VERIFY:
119 ret = PackCertificateVerify(ctx, buf, bufLen, usedLen);
120 break;
121 case FINISHED:
122 ret = PackFinished(ctx, buf, bufLen, usedLen);
123 break;
124 #ifdef HITLS_TLS_FEATURE_KEY_UPDATE
125 case KEY_UPDATE:
126 ret = PackKeyUpdate(ctx, buf, bufLen, usedLen);
127 break;
128 #endif
129 default:
130 ret = HITLS_PACK_UNSUPPORT_HANDSHAKE_MSG;
131 break;
132 }
133
134 if (ret != HITLS_SUCCESS) {
135 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15813, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
136 "pack handshake[%u] msg error.", type, 0, 0, 0);
137 }
138 return ret;
139 }
140 #endif /* HITLS_TLS_PROTO_TLS13 */
141 #ifdef HITLS_TLS_PROTO_DTLS12
Dtls12PackMsg(TLS_Ctx * ctx,HS_MsgType type,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)142 int32_t Dtls12PackMsg(TLS_Ctx *ctx, HS_MsgType type, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
143 {
144 uint16_t sequence = 0;
145 int32_t ret = HITLS_SUCCESS;
146 uint32_t len = 0;
147
148 ret = PackHsMsgBody(ctx, type, &buf[DTLS_HS_MSG_HEADER_SIZE], bufLen - DTLS_HS_MSG_HEADER_SIZE, &len);
149 if (ret != HITLS_SUCCESS) {
150 return ret;
151 }
152 sequence = ctx->hsCtx->nextSendSeq;
153 PackDtlsMsgHeader(type, sequence, len, buf);
154
155 *usedLen = len + DTLS_HS_MSG_HEADER_SIZE;
156
157 return HITLS_SUCCESS;
158 }
159 #endif
160 #ifdef HITLS_TLS_PROTO_TLS_BASIC
Tls12PackMsg(TLS_Ctx * ctx,HS_MsgType type,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)161 int32_t Tls12PackMsg(TLS_Ctx *ctx, HS_MsgType type, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
162 {
163 int32_t ret = HITLS_SUCCESS;
164 uint32_t len = 0;
165
166 if (type > HS_MSG_TYPE_END) {
167 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16943, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "type err", 0, 0, 0, 0);
168 BSL_ERR_PUSH_ERROR(HITLS_PACK_UNSUPPORT_HANDSHAKE_MSG);
169 return HITLS_PACK_UNSUPPORT_HANDSHAKE_MSG;
170 }
171 ret = PackHsMsgBody(ctx, type, &buf[HS_MSG_HEADER_SIZE], bufLen - HS_MSG_HEADER_SIZE, &len);
172 if (ret != HITLS_SUCCESS) {
173 return ret;
174 }
175
176 buf[0] = (uint8_t)type & 0xffu; /* Fill handshake message type */
177 BSL_Uint24ToByte(len, &buf[1]); /* Fill handshake message body length */
178
179 *usedLen = len + HS_MSG_HEADER_SIZE;
180
181 return HITLS_SUCCESS;
182 }
183 #endif /* HITLS_TLS_PROTO_TLS_BASIC */
184 #ifdef HITLS_TLS_PROTO_TLS13
Tls13PackMsg(TLS_Ctx * ctx,HS_MsgType type,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)185 int32_t Tls13PackMsg(TLS_Ctx *ctx, HS_MsgType type, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
186 {
187 int32_t ret = HITLS_SUCCESS;
188 uint32_t len = 0;
189 int32_t enumBorder = HS_MSG_TYPE_END;
190 if ((int32_t)type > enumBorder) {
191 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16944, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "type err", 0, 0, 0, 0);
192 return HITLS_PACK_UNSUPPORT_HANDSHAKE_MSG;
193 }
194 ret = PackTls13HsMsgBody(ctx, type, &buf[HS_MSG_HEADER_SIZE], bufLen - HS_MSG_HEADER_SIZE, &len);
195 if (ret != HITLS_SUCCESS) {
196 return ret;
197 }
198
199 buf[0] = (uint8_t)type & 0xffu; /* Fill handshake message type */
200 BSL_Uint24ToByte(len, &buf[1]); /* Fill handshake message body length */
201
202 *usedLen = len + HS_MSG_HEADER_SIZE;
203
204 return HITLS_SUCCESS;
205 }
206 #endif /* HITLS_TLS_PROTO_TLS13 */
HS_PackMsg(TLS_Ctx * ctx,HS_MsgType type,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)207 int32_t HS_PackMsg(TLS_Ctx *ctx, HS_MsgType type, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
208 {
209 if ((ctx == NULL) || (buf == NULL) || (usedLen == NULL) || (bufLen == 0)) {
210 BSL_ERR_PUSH_ERROR(HITLS_INTERNAL_EXCEPTION);
211 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15814, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
212 "the input parameter pointer is null.", 0, 0, 0, 0);
213 return HITLS_INTERNAL_EXCEPTION;
214 }
215
216 uint32_t version = HS_GetVersion(ctx);
217
218 switch (version) {
219 #ifdef HITLS_TLS_PROTO_TLS_BASIC
220 case HITLS_VERSION_TLS12:
221 #ifdef HITLS_TLS_PROTO_TLCP11
222 case HITLS_VERSION_TLCP_DTLCP11:
223 #if defined(HITLS_TLS_PROTO_DTLCP11)
224 if (IS_SUPPORT_DATAGRAM(ctx->config.tlsConfig.originVersionMask)) {
225 return Dtls12PackMsg(ctx, type, buf, bufLen, usedLen);
226 }
227 #endif
228 #endif
229 return Tls12PackMsg(ctx, type, buf, bufLen, usedLen);
230 #endif /* HITLS_TLS_PROTO_TLS_BASIC */
231 #ifdef HITLS_TLS_PROTO_TLS13
232 case HITLS_VERSION_TLS13:
233 return Tls13PackMsg(ctx, type, buf, bufLen, usedLen);
234 #endif /* HITLS_TLS_PROTO_TLS13 */
235 #ifdef HITLS_TLS_PROTO_DTLS12
236 case HITLS_VERSION_DTLS12:
237 return Dtls12PackMsg(ctx, type, buf, bufLen, usedLen);
238 #endif
239 default:
240 break;
241 }
242
243 BSL_ERR_PUSH_ERROR(HITLS_PACK_UNSUPPORT_VERSION);
244 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15815, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
245 "pack handshake msg error, unsupport version[0x%x].", version, 0, 0, 0);
246 return HITLS_PACK_UNSUPPORT_VERSION;
247 }
248