* This file is part of the openHiTLS project.
*
* openHiTLS is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#ifndef HS_CTX_H
#define HS_CTX_H
#include <stdint.h>
#include "hitls_build.h"
#include "hitls_crypt_type.h"
#include "cert.h"
#include "crypt.h"
#include "rec.h"
#include "hs_msg.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MASTER_SECRET_LEN 48u
#define COOKIE_SECRET_LIFETIME 5u
#ifndef HITLS_HS_INIT_BUFFER_SIZE
#define HITLS_HS_INIT_BUFFER_SIZE 4096u
#endif
#ifndef HITLS_HS_BUFFER_SIZE_LIMIT
#define HITLS_HS_BUFFER_SIZE_LIMIT 20480u
#endif
#if HITLS_HS_INIT_BUFFER_SIZE < 32
#error "HITLS_HS_INIT_BUFFER_SIZE must be greater than or equal to 32"
#endif
#if HITLS_HS_BUFFER_SIZE_LIMIT < HITLS_HS_INIT_BUFFER_SIZE
#error "HITLS_HS_BUFFER_SIZE_LIMIT must be greater than or equal to HITLS_HS_INIT_BUFFER_SIZE"
#endif
typedef struct {
HITLS_ECParameters curveParams;
} EcdhParam;
typedef struct {
uint8_t *p;
uint8_t *g;
uint16_t plen;
uint16_t glen;
} DhParam;
typedef struct {
uint8_t preMasterSecret[MASTER_SECRET_LEN];
} RsaParam;
typedef struct {
uint8_t preMasterSecret[MASTER_SECRET_LEN];
} EccParam;
typedef struct {
uint16_t groups[MAX_KEYSHARE_COUNT];
uint8_t count;
} KeyShareParam;
* @ingroup hitls
*
* @brief PskInfo is used for PSK negotiation and stores identity and psk during negotiation
*/
#ifdef HITLS_TLS_FEATURE_PSK
typedef struct {
uint8_t *identity;
uint32_t identityLen;
uint8_t *psk;
uint32_t pskLen;
} PskInfo;
#endif
#ifdef HITLS_TLS_PROTO_TLS13
typedef struct {
uint8_t *identity;
uint32_t identityLen;
HITLS_Session *pskSession;
uint8_t num;
} UserPskList;
typedef struct {
UserPskList *userPskSess;
HITLS_Session *resumeSession;
int32_t selectIndex;
uint8_t *psk;
uint32_t pskLen;
} PskInfo13;
#endif
typedef struct {
HITLS_KeyExchAlgo keyExchAlgo;
union {
EcdhParam ecdh;
DhParam dh;
RsaParam rsa;
EccParam ecc;
KeyShareParam share;
} keyExchParam;
HITLS_CRYPT_Key *key;
HITLS_CRYPT_Key *keys[MAX_KEYSHARE_COUNT];
uint8_t *peerPubkey;
uint32_t pubKeyLen;
#ifdef HITLS_TLS_FEATURE_PSK
PskInfo *pskInfo;
#endif
#ifdef HITLS_TLS_PROTO_TLS13
PskInfo13 pskInfo13;
uint8_t *ciphertext;
uint32_t ciphertextLen;
#endif
} KeyExchCtx;
typedef struct HsMsgCache {
uint8_t *data;
uint32_t dataSize;
struct HsMsgCache *next;
} HsMsgCache;
typedef struct {
HITLS_HashAlgo hashAlgo;
HITLS_HASH_Ctx *hashCtx;
uint8_t *verifyData;
uint32_t verifyDataSize;
uint32_t verifyDataCapacity;
HsMsgCache *dataBuf;
} VerifyCtx;
struct HsCtx {
HITLS_HandshakeState state;
HitlsProcessState readSubState;
HS_Msg *hsMsg;
ExtensionFlag extFlag;
#ifdef HITLS_TLS_PROTO_TLS13
HITLS_HandshakeState ccsNextState;
bool haveHrr;
#endif
bool isNeedClientCert;
#if defined(HITLS_TLS_FEATURE_SESSION) || defined(HITLS_TLS_PROTO_TLS13)
uint32_t sessionIdSize;
uint8_t *sessionId;
#endif
uint8_t *clientRandom;
uint8_t *serverRandom;
#ifdef HITLS_TLS_PROTO_TLS13
uint8_t earlySecret[MAX_DIGEST_SIZE];
uint8_t handshakeSecret[MAX_DIGEST_SIZE];
#endif
uint8_t masterKey[MAX_DIGEST_SIZE];
CERT_Pair *peerCert;
#ifdef HITLS_TLS_FEATURE_ALPN
uint8_t *clientAlpnList;
uint32_t clientAlpnListSize;
#endif
#ifdef HITLS_TLS_FEATURE_SESSION_TICKET
uint32_t ticketSize;
uint8_t *ticket;
uint32_t ticketLifetimeHint;
#ifdef HITLS_TLS_PROTO_TLS13
uint32_t ticketAgeAdd;
uint64_t nextTicketNonce;
uint32_t sentTickets;
#endif
#endif
KeyExchCtx *kxCtx;
VerifyCtx *verifyCtx;
uint8_t *msgBuf;
uint32_t msgOffset;
uint32_t bufferLen;
uint32_t msgLen;
#ifdef HITLS_TLS_PROTO_TLS13
uint8_t clientHsTrafficSecret[MAX_DIGEST_SIZE];
client */
uint8_t serverHsTrafficSecret[MAX_DIGEST_SIZE];
server */
ClientHelloMsg *firstClientHello;
#endif
#ifdef HITLS_TLS_PROTO_DTLS12
uint16_t nextSendSeq;
uint16_t expectRecvSeq;
HS_ReassQueue *reassMsg;
#ifdef HITLS_BSL_UIO_UDP
uint32_t timeoutValue;
uint32_t timeoutNum;
#endif
#endif
};
#ifdef __cplusplus
}
#endif
#endif