API参考
载入中...
搜索中...
未找到
hs_ctx.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_CTX_H
17#define HS_CTX_H
18
19#include <stdint.h>
20#include "hitls_build.h"
21#include "hitls_crypt_type.h"
22#include "cert.h"
23#include "crypt.h"
24#include "rec.h"
25#include "hs_msg.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#define MASTER_SECRET_LEN 48u
32#define COOKIE_SECRET_LIFETIME 5u /* the number of times the cookie's secret is used */
33
34#ifndef HITLS_HS_INIT_BUFFER_SIZE
35#define HITLS_HS_INIT_BUFFER_SIZE 4096u
36#endif
37
38#ifndef HITLS_HS_BUFFER_SIZE_LIMIT
39#define HITLS_HS_BUFFER_SIZE_LIMIT 20480u
40#endif
41
42#if HITLS_HS_INIT_BUFFER_SIZE < 32
43#error "HITLS_HS_INIT_BUFFER_SIZE must be greater than or equal to 32"
44#endif
45
46#if HITLS_HS_BUFFER_SIZE_LIMIT < HITLS_HS_INIT_BUFFER_SIZE
47#error "HITLS_HS_BUFFER_SIZE_LIMIT must be greater than or equal to HITLS_HS_INIT_BUFFER_SIZE"
48#endif
49
50/* Transmits ECDH key exchange data */
51typedef struct {
52 HITLS_ECParameters curveParams; /* Elliptic curve parameter */
53} EcdhParam;
54
55/* Transmits DH key exchange data */
56typedef struct {
57 uint8_t *p; /* prime */
58 uint8_t *g; /* generator */
59 uint16_t plen; /* prime length */
60 uint16_t glen; /* generator length */
61} DhParam;
62
63/* Used to transfer RSA key exchange data */
64typedef struct {
65 uint8_t preMasterSecret[MASTER_SECRET_LEN];
66} RsaParam;
67
68/* Used to transfer Ecc key exchange data */
69typedef struct {
70 uint8_t preMasterSecret[MASTER_SECRET_LEN];
71} EccParam;
72
73typedef struct {
74 /* For TLS1.3 multi-key share, we try to send two key shares:
75 * - One for key encapsulation mechanism (KEM)
76 * - One for key exchange (KEX) */
77 HITLS_NamedGroup group; /* First group for key share */
78 HITLS_NamedGroup secondGroup; /* Second group for key share */
80
86#ifdef HITLS_TLS_FEATURE_PSK
87typedef struct {
88 uint8_t *identity;
89 uint32_t identityLen;
90 uint8_t *psk;
91 uint32_t pskLen;
92} PskInfo;
93#endif /* HITLS_TLS_FEATURE_PSK */
94#ifdef HITLS_TLS_PROTO_TLS13
95typedef struct {
96 uint8_t *identity;
97 uint32_t identityLen;
98 HITLS_Session *pskSession;
99 uint8_t num;
100} UserPskList;
101
102typedef struct {
103 UserPskList *userPskSess; /* tls 1.3 user psk session */
104 HITLS_Session *resumeSession; /* tls 1.3 psk resume */
105 int32_t selectIndex; /* selected index */
106 uint8_t *psk; /* selected psk */
107 uint32_t pskLen;
108} PskInfo13;
109#endif /* HITLS_TLS_PROTO_TLS13 */
110
111/* Used to transfer the key exchange context */
112typedef struct {
113 HITLS_KeyExchAlgo keyExchAlgo;
114 union {
115 EcdhParam ecdh;
116 DhParam dh;
117 RsaParam rsa;
118 EccParam ecc; /* Sm2 parameter */
119 KeyShareParam share;
120 } keyExchParam;
121 HITLS_CRYPT_Key *key; /* Local key pair */
122 HITLS_CRYPT_Key *secondKey; /* second key pair for tls1.3 multi-key share */
123 uint8_t *peerPubkey; /* peer public key or peer ciphertext */
124 uint32_t pubKeyLen; /* peer public key length */
125#ifdef HITLS_TLS_FEATURE_PSK
126 PskInfo *pskInfo; /* PSK data tls 1.2 */
127#endif /* HITLS_TLS_FEATURE_PSK */
128#ifdef HITLS_TLS_PROTO_TLS13
129 PskInfo13 pskInfo13; /* tls 1.3 psk */
130 uint8_t *ciphertext; /* local ciphertext */
131 uint32_t ciphertextLen; /* ciphertext length */
132#endif /* HITLS_TLS_PROTO_TLS13 */
133} KeyExchCtx;
134
135/* Buffer for transmitting handshake data. */
136typedef struct HsMsgCache {
137 uint8_t *data;
138 uint32_t dataSize;
139 struct HsMsgCache *next;
140} HsMsgCache;
141
142/* Used to transfer the handshake data verification context. */
143typedef struct {
144 HITLS_HashAlgo hashAlgo;
145 HITLS_HASH_Ctx *hashCtx;
146 uint8_t verifyData[MAX_SIGN_SIZE];
147 uint32_t verifyDataSize;
148 HsMsgCache *dataBuf; /* handshake data buffer */
149} VerifyCtx;
150
151/* Used to pass the handshake context */
152struct HsCtx {
153 HITLS_HandshakeState state;
154 HitlsProcessState readSubState;
155 HS_Msg *hsMsg;
156 ExtensionFlag extFlag;
157#ifdef HITLS_TLS_PROTO_TLS13
158 HITLS_HandshakeState ccsNextState;
159 bool haveHrr; /* Whether the hello retry request has been processed */
160#endif
161 bool isNeedClientCert;
162#if defined(HITLS_TLS_FEATURE_SESSION) || defined(HITLS_TLS_PROTO_TLS13)
163 uint32_t sessionIdSize;
164 uint8_t *sessionId;
165#endif
166 uint8_t *clientRandom;
167 uint8_t *serverRandom;
168#ifdef HITLS_TLS_PROTO_TLS13
169 uint8_t earlySecret[MAX_DIGEST_SIZE];
170 uint8_t handshakeSecret[MAX_DIGEST_SIZE];
171#endif
172 uint8_t masterKey[MAX_DIGEST_SIZE];
173 CERT_Pair *peerCert;
174#ifdef HITLS_TLS_FEATURE_ALPN
175 uint8_t *clientAlpnList;
176 uint32_t clientAlpnListSize;
177#endif
178#ifdef HITLS_TLS_FEATURE_SESSION_TICKET
179 uint32_t ticketSize;
180 uint8_t *ticket;
181 uint32_t ticketLifetimeHint; /* ticket timeout interval, in seconds */
182#ifdef HITLS_TLS_PROTO_TLS13
183 uint32_t ticketAgeAdd; /* Used to obfuscate ticket age */
184
185 uint64_t nextTicketNonce; /* TLS1.3 connection, starting from 0 and increasing in ascending order */
186 uint32_t sentTickets; /* TLS1.3 Number of tickets sent */
187#endif /* HITLS_TLS_PROTO_TLS13 */
188#endif /* HITLS_TLS_FEATURE_SESSION_TICKET */
189 KeyExchCtx *kxCtx; /* Key Exchange Context */
190 VerifyCtx *verifyCtx; /* Verify the context of handshake data. */
191 uint8_t *msgBuf; /* Buffer for receiving and sending messages */
192 uint32_t msgOffset; /* messages offset */
193 uint32_t bufferLen; /* messages buffer size */
194 uint32_t msgLen; /* Total length of buffered messages */
195#ifdef HITLS_TLS_PROTO_TLS13
196 uint8_t clientHsTrafficSecret[MAX_DIGEST_SIZE]; /* Handshake secret used to encrypt the message sent by the TLS1.3
197 client */
198 uint8_t serverHsTrafficSecret[MAX_DIGEST_SIZE]; /* Handshake secret used to encrypt the message sent by the TLS1.3
199 server */
200 ClientHelloMsg *firstClientHello; /* TLS1.3 server records the first received ClientHello message */
201#endif /* HITLS_TLS_PROTO_TLS13 */
202#ifdef HITLS_TLS_PROTO_DTLS12
203 uint16_t nextSendSeq; /* message sending sequence number */
204 uint16_t expectRecvSeq; /* message receiving sequence number */
205 HS_ReassQueue *reassMsg; /* reassembly message queue, used for reassembly of fragmented messages */
206#ifdef HITLS_BSL_UIO_UDP
207 uint32_t timeoutValue; /* Timeout interval, in us. */
208 uint32_t timeoutNum; /* Timeout count */
209#endif /* HITLS_BSL_UIO_UDP */
210#endif /* HITLS_TLS_PROTO_DTLS12 */
211};
212
213#ifdef __cplusplus
214}
215#endif /* end __cplusplus */
216#endif /* end HS_CTX_H */
定义 cert_mgr.h:32
定义 hs_msg.h:166
定义 hs_ctx.h:56
定义 hs_ctx.h:69
定义 hs_ctx.h:51
定义 hs_msg.h:137
Elliptic curve parameter
定义 hitls_crypt_type.h:222
定义 hs_msg.h:369
定义 hs_ctx.h:152
定义 hs_ctx.h:136
PskInfo is used for PSK negotiation and stores identity and psk during negotiation
定义 hs_ctx.h:112
定义 hs_ctx.h:73
定义 hs_ctx.h:64
定义 hs_ctx.h:143