API参考
载入中...
搜索中...
未找到
hs_msg.h
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#ifndef HS_MSG_H
17#define HS_MSG_H
18
19#include <stdint.h>
20#include <stdbool.h>
21#include "hitls_build.h"
22#include "bsl_module_list.h"
23#include "cert.h"
24#include "hitls_crypt_type.h"
25#include "hitls_cert_type.h"
26#include "hitls_type.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31#define UINT24_SIZE 3u
32#define HS_MSG_HEADER_SIZE 4u
33#define DTLS_HS_MSG_HEADER_SIZE 12u
34#define HS_RANDOM_SIZE 32u
35#define HS_RANDOM_DOWNGRADE_SIZE 8u
36#define TLS_HS_MAX_SESSION_ID_SIZE 32u
37#define TLS_HS_MIN_SESSION_ID_SIZE 24u
38#define TLS_HS_MIN_COOKIE_SIZE 1u
39#define TLS_HS_MAX_COOKIE_SIZE 255u
40
41#define DTLS_HS_MSGLEN_ADDR 1u /* DTLS message length address, which is used when parsing the DTLS message header. */
42/* DTLS message sequence number address, which is used for parsing the DTLS message header. */
43#define DTLS_HS_MSGSEQ_ADDR 4u
44/* DTLS message fragment offset address, which is used when the DTLS message header is parsed. */
45#define DTLS_HS_FRAGMENT_OFFSET_ADDR 6u
46/* DTLS message fragment length address, which is used when parsing the DTLS message header. */
47#define DTLS_HS_FRAGMENT_LEN_ADDR 9u
48
49/* Handshake message type */
50typedef enum {
51 HELLO_REQUEST = 0,
52 CLIENT_HELLO = 1,
53 SERVER_HELLO = 2,
54 HELLO_VERIFY_REQUEST = 3,
55 NEW_SESSION_TICKET = 4,
56 END_OF_EARLY_DATA = 5,
57 HELLO_RETRY_REQUEST = 6,
58 ENCRYPTED_EXTENSIONS = 8,
59 CERTIFICATE = 11,
60 SERVER_KEY_EXCHANGE = 12,
61 CERTIFICATE_REQUEST = 13,
62 SERVER_HELLO_DONE = 14,
63 CERTIFICATE_VERIFY = 15,
64 CLIENT_KEY_EXCHANGE = 16,
65 FINISHED = 20,
66 CERTIFICATE_URL = 21,
67 CERTIFICATION_STATUS = 22,
68 SUPPLEMENTAL_DATA = 23,
69 KEY_UPDATE = 24,
70 MESSAGE_HASH = 254,
71 HS_MSG_TYPE_END = 255
72} HS_MsgType;
73
74typedef enum {
75 PSK_KE = 0,
76 PSK_DHE_KE = 1,
77 PSK_KEY_EXCHANGEMODE_END = 255
78} HS_PskKeyExchMode;
79
80typedef struct {
81 HITLS_KeyUpdateRequest requestUpdate;
83
84typedef struct {
85 ListHead head;
86 uint16_t group; /* Naming group of keys to be exchanged */
87 uint16_t keyExchangeSize;
88 uint8_t *keyExchange; /* Key exchange information */
89} KeyShare;
90
91typedef struct OfferedPsks {
92 ListHead pskNode; /* Multiple PSK linked lists are formed through pskNode. The actual data of this node is the
93 following fields */
94 uint8_t *identity; /* pskid and binder are in one-to-one mapping. */
95 uint8_t *binder; /* HMAC value */
96 uint32_t obfuscatedTicketAge; /* An obfuscated version of the age of the key */
97 uint16_t identitySize; /* bytes of identity */
98 uint8_t binderSize; /* bytes of binder */
99 bool isValid; /* is binder valid */
100} PreSharedKey;
101
102typedef struct {
103 uint16_t *supportedGroups;
104 uint16_t *signatureAlgorithms;
105 uint16_t *signatureAlgorithmsCert;
106 uint8_t *pointFormats;
107 uint8_t *alpnList; /* application-layer protocol negotiation list */
108 uint8_t *serverName; /* serverName after parsing */
109 uint8_t *secRenegoInfo; /* renegotiation extension information */
110 uint8_t *ticket; /* ticket information */
111
112 uint32_t ticketSize;
113 uint16_t supportedGroupsSize;
114 uint16_t signatureAlgorithmsSize;
115 uint16_t signatureAlgorithmsCertSize;
116 uint16_t alpnListSize; /* application-layer protocol negotiation list len */
117 uint16_t serverNameSize;
118 uint16_t recordSizeLimit;
119 uint8_t pointFormatsSize;
120 uint8_t serverNameType; /* Type of the parsed serverName. */
121 uint8_t secRenegoInfoSize; /* Length of the security renegotiation information */
122 uint8_t reserved[1]; /* Four-byte alignment */
123
124 /* TLS1.3 */
125 uint16_t *supportedVersions;
126 uint8_t *cookie;
127 uint8_t *keModes;
128 uint8_t keModesSize;
129 uint8_t supportedVersionsCount; /* Number of supported version */
130 uint16_t cookieLen;
131
132 HITLS_TrustedCAList *caList;
133 PreSharedKey *preSharedKey;
134 KeyShare *keyShare; /* In the ClientHello message, this extension provides a set of KeyShares */
136
137typedef struct {
138 bool haveSupportedGroups;
139 bool haveSignatureAlgorithms;
140 bool haveSignatureAlgorithmsCert;
141 bool havePointFormats;
142 bool haveExtendedMasterSecret;
143 bool haveSupportedVers;
144 bool haveCookie; /* Whether there is a cookie (involved in TLS1.3 ClientHello) */
145 bool haveCA; /* Whether the CA exists (involved in TLS1.3 ClientHello) */
146 bool havePostHsAuth; /* Indicates whether the Client (TLS1.3) is willing to receive the Certificate Request
147 message. */
148 bool haveKeyShare;
149 bool haveEarlyData;
150 bool havePskExMode; /* Indicates whether the TLS1.3 key exchange mode exists. */
151 bool havePreShareKey; /* Indicates whether the pre-shared key exists. */
152 bool haveAlpn; /* Whether there is Alpn */
153 bool haveServerName; /* Whether the ServerName extension exists. */
154 bool haveSecRenego; /* Whether security renegotiation exists. */
155 bool haveTicket; /* Indicates whether a ticket is available. */
156 bool haveEncryptThenMac; /* Indicates whether EncryptThenMac is supported. */
157 bool haveRecordSizeLimit;
159
160typedef struct {
161 ExtensionFlag flag;
162 ExtensionContent content;
164
165/* It is used to transmit client hello message */
166typedef struct {
167 uint8_t randomValue[HS_RANDOM_SIZE]; /* random number group */
168 uint8_t *sessionId;
169 uint8_t *cookie; /* Cookie (for DTLS only) */
170 uint16_t *cipherSuites;
171 uint16_t version;
172 uint16_t cipherSuitesSize;
173 uint8_t sessionIdSize;
174 uint8_t compressionMethodsSize;
175 uint8_t *compressionMethods;
176 uint8_t cookieLen;
177 bool haveEmptyRenegoScsvCipher; /* According to RFC 5746, a special signaling cipher suite value (SCSV) can be used
178 to indicate that security renegotiation is supported. */
179 bool haveFallBackScsvCipher; /* According to RFC 7507, a special signaling cipher suite value (SCSV) can be used
180 to indicate that a downgrade negotiation process is in progress. */
181 uint8_t refCnt; /* Do not involve multiple threads. Process the hrr check clientHello. */
182 uint32_t truncateHelloLen; /* is used for binder calculation. */
183 ClientHelloExt extension;
184 uint64_t extensionTypeMask;
185 uint8_t *extensionBuff;
186 uint32_t extensionBuffLen;
187 uint8_t extensionCount; /* Size of the extension buffer */
189
190/* It is used to transmit server hello message */
191typedef struct {
192 uint16_t version;
193 uint16_t cipherSuite;
194 uint8_t randomValue[HS_RANDOM_SIZE]; /* random number group */
195 uint8_t *sessionId;
196 uint8_t *pointFormats;
197 uint8_t *alpnSelected; /* selected alpn protocol */
198 uint8_t *cookie;
199 uint8_t *secRenegoInfo;
200 KeyShare keyShare;
201 uint16_t alpnSelectedSize; /* selected alpn protocol length */
202 uint16_t supportedVersion;
203 uint16_t cookieLen;
204 uint16_t selectedIdentity; /* TLS 1.3 psk required */
205 uint16_t recordSizeLimit;
206 uint8_t sessionIdSize;
207 uint8_t pointFormatsSize;
208 uint8_t secRenegoInfoSize; /* Length of the security renegotiation information */
209 uint64_t extensionTypeMask;
210 bool havePointFormats;
211 bool haveExtendedMasterSecret;
212 bool haveSupportedVersion;
213 bool haveCookie; /* Indicates whether the cookie length is involved in TLS1.3 HelloRetryRequest. */
214 bool haveKeyShare; /* Whether KeyShare is extended. */
215 bool haveSelectedIdentity; /* Indicates whether the Pre_PSK is selected. */
216 bool haveSelectedAlpn; /* Whether the application layer protocol is selected. */
217 bool haveServerName;
218 bool haveSecRenego;
219 bool haveTicket;
220 bool haveEncryptThenMac;
221 bool haveRecordSizeLimit;
222 bool reserved[2]; /* Four-byte alignment */
224
225/* It is used to transmit hello verify request message */
226typedef struct {
227 uint16_t version;
228 uint8_t cookieLen;
229 uint8_t reserved[1]; /* fill with 1 byte for 4-byte alignment */
230 uint8_t *cookie;
232
233/* Transmits certificate message */
234typedef struct {
235 CERT_Item *cert; /* Certificate message content */
236 uint32_t certCount; /* Number of certificates */
237 uint8_t *certificateReqCtx; /* Used by the TLS 1.3 */
238 uint32_t certificateReqCtxSize; /* Used by the TLS 1.3 */
239 uint64_t extensionTypeMask; /* Used by the TLS 1.3 */
241
242typedef struct {
243 HITLS_ECParameters ecPara; /* Elliptic curve field parameter of the ECDH public key */
244 uint32_t pubKeySize; /* Length of the ecdh public key */
245 uint8_t *pubKey; /* ecdh public key content */
246 uint16_t signAlgorithm;
247 uint16_t signSize;
248 uint8_t *signData;
249} ServerEcdh;
250typedef struct {
251 uint8_t *p;
252 uint8_t *g;
253 uint16_t plen;
254 uint16_t glen;
255 uint8_t *pubkey;
256 uint16_t pubKeyLen;
257 uint16_t signAlgorithm;
258 uint16_t signSize;
259 uint8_t *signData;
260} ServerDh;
261
262/* Used to transfer the key exchange content of the server */
263typedef struct {
264 uint8_t *pskIdentityHint; /* psk identity negotiation prompt message */
265 uint32_t hintSize;
266 HITLS_KeyExchAlgo keyExType; /* key exchange mode */
267 union {
268 ServerEcdh ecdh;
269 ServerDh dh;
270 } keyEx;
272
273/* Used to transfer the client key exchange content */
274typedef struct {
275 uint8_t *pskIdentity;
276 uint32_t pskIdentitySize;
277 uint32_t dataSize; /* Key exchange data length */
278 uint8_t *data; /* Key exchange data. */
280
281/* Transmits certificate request message */
282typedef struct {
283 uint8_t *certTypes;
284 uint16_t *signatureAlgorithms;
285 uint8_t reserved; /* Four-byte alignment */
286 uint8_t certTypesSize;
287 uint16_t signatureAlgorithmsSize;
288#ifdef HITLS_TLS_PROTO_TLS13
289 uint16_t *signatureAlgorithmsCert;
290 uint16_t signatureAlgorithmsCertSize;
291 uint8_t *certificateReqCtx; /* Used by the TLS 1.3 */
292 uint32_t certificateReqCtxSize; /* This field is used by the TLS 1.3. The value is not 0 only for the
293 authentication after the handshake */
294 uint64_t extensionTypeMask;
295 bool haveSignatureAndHashAlgoCert;
296#endif /* HITLS_TLS_PROTO_TLS13 */
297 bool haveSignatureAndHashAlgo;
298 bool haveDistinguishedName;
300
301/* Transmits certificate verification message */
302typedef struct {
303 uint16_t signHashAlg; /* Signature hash algorithm, which is available only for TLS1.2 and DTLS1.2 */
304 uint16_t signSize; /* Length of the signature data. */
305 uint8_t *sign; /* Signature data */
307
308/* It is used to transmit Ticket message
309 RFC5077 3.3 NewSessionTicket Handshake Message
310 struct {
311 uint32 ticket_lifetime_hint;
312 opaque ticket<0..2^16-1>;
313 } NewSessionTicket;
314
315 TLS1.3:
316 struct {
317 uint32 ticket_lifetime;
318 uint32 ticket_age_add;
319 opaque ticket_nonce<0..255>;
320 opaque ticket<1..2^16-1>;
321 Extension extensions<0..2^16-2>;
322 } NewSessionTicket;
323*/
324typedef struct {
325 uint32_t ticketLifetimeHint; /* ticket timeout interval, in seconds */
326 uint32_t ticketAgeAdd; /* ticket_age_add: a random number generated each time a ticket is issued. */
327 uint32_t ticketNonceSize; /* ticket_nonce length */
328 uint8_t *ticketNonce; /* ticketNonce: Unique ID of the ticket issued on the connection, starting from 0 and
329 increasing in ascending order. */
330 uint32_t ticketSize;
331 uint8_t *ticket; /* ticket */
332 uint64_t extensionTypeMask;
334
335/* It is used to transmit finish message */
336typedef struct {
337 uint32_t verifyDataSize;
338 uint8_t *verifyData;
340
341typedef struct {
342 uint16_t *supportedGroups;
343 uint16_t supportedGroupsSize;
344 uint16_t recordSizeLimit;
345 uint16_t alpnSelectedSize; /* selected alpn protocol length */
346 uint8_t *alpnSelected; /* selected alpn protocol */
347 uint64_t extensionTypeMask;
348
349 bool haveSupportedGroups;
350 bool haveEarlyData;
351 bool haveServerName;
352 bool haveRecordSizeLimit;
353 bool haveSelectedAlpn;
355
356/* Used to parse the handshake message header. */
357typedef struct {
358 HS_MsgType type;
359 uint32_t length; /* handshake msg body length */
360 uint16_t sequence; /* DTLS Indicates the number of the handshake message. Each time a new handshake message is
361 sent, one is added. Retransmission does not add up */
362 uint32_t fragmentOffset; /* Fragment offset of DTLS handshake message */
363 uint32_t fragmentLength; /* Fragment length of the DTLS handshake message */
364 const uint8_t *rawMsg; /* Complete handshake information */
365 uint32_t headerAndBodyLen;
366} HS_MsgInfo;
367
368/* It is used to transmit handshake message */
369typedef struct {
370 HS_MsgType type;
371 uint32_t length;
372 uint16_t sequence; /* DTLS Indicates the number of the handshake message. Each time a new handshake message is
373 sent, one is added. Retransmission does not add up */
374 uint8_t reserved[2]; /* fill 2 bytes for 4-byte alignment. */
375 uint32_t fragmentOffset; /* Fragment offset of DTLS handshake message. */
376 uint32_t fragmentLength; /* Fragment length of the DTLS handshake message */
377 union {
378 ClientHelloMsg clientHello;
379 ServerHelloMsg serverHello;
380 HelloVerifyRequestMsg helloVerifyReq;
381 EncryptedExtensions encryptedExtensions;
382 CertificateMsg certificate;
383 ClientKeyExchangeMsg clientKeyExchange;
384 ServerKeyExchangeMsg serverKeyExchange;
385 CertificateRequestMsg certificateReq;
386 CertificateVerifyMsg certificateVerify;
387 NewSessionTicketMsg newSessionTicket;
388 FinishedMsg finished;
389 KeyUpdateMsg keyUpdate;
390 } body;
391} HS_Msg;
392
393#ifdef HITLS_TLS_PROTO_DTLS12
394/* Reassembles fragmented messages */
395typedef struct {
396 ListHead head;
397 HS_MsgType type;
398 uint16_t sequence; /* DTLS Indicates the number of the handshake message. Each time a new handshake message is
399 sent, one is added. Retransmission does not add up */
400 bool isReassComplete; /* Indicates whether the message is reassembled. */
401 uint8_t reserved; /* Padded with 1 byte for 4-byte alignment. */
402 uint8_t *reassBitMap; /* bitmap, used for processing duplicate fragmented message and calculating whether the
403 fragmented message are completely reassembled. */
404 uint8_t *msg; /* Used to store the handshake messages during the reassembly. */
405 uint32_t msgLen; /* Total length of a message, including the message header. */
406} HS_ReassQueue;
407#endif
408
409#ifdef __cplusplus
410}
411#endif /* end __cplusplus */
412
413#endif /* end HS_MSG_H */
struct BslList HITLS_TrustedCAList
Describes the list of trusted CAs
定义 hitls_cert_type.h:67
HITLS_KeyUpdateRequest
key update message type
定义 hitls_type.h:147
定义 hs_msg.h:234
定义 hs_msg.h:282
定义 hs_msg.h:302
定义 hs_msg.h:160
定义 hs_msg.h:166
定义 hs_msg.h:274
定义 hs_msg.h:341
定义 hs_msg.h:102
定义 hs_msg.h:137
定义 hs_msg.h:336
Elliptic curve parameter
定义 hitls_crypt_type.h:222
定义 hs_msg.h:357
定义 hs_msg.h:369
定义 hs_msg.h:226
定义 hs_msg.h:84
定义 hs_msg.h:80
定义 hs_msg.h:324
定义 hs_msg.h:91
定义 hs_msg.h:250
定义 hs_msg.h:242
定义 hs_msg.h:191
定义 hs_msg.h:263