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 RECORD_H 17 #define RECORD_H 18 19 #include "tls.h" 20 #include "rec.h" 21 #include "rec_header.h" 22 #include "rec_unprocessed_msg.h" 23 #include "rec_buf.h" 24 #include "rec_conn.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 #define REC_MAX_PLAIN_TEXT_LENGTH 16384 /* Plain content length */ 31 32 #define REC_MAX_ENCRYPTED_OVERHEAD 2048u /* Maximum Encryption Overhead rfc5246 */ 33 #define REC_MAX_READ_ENCRYPTED_OVERHEAD REC_MAX_ENCRYPTED_OVERHEAD 34 #define REC_MAX_WRITE_ENCRYPTED_OVERHEAD REC_MAX_ENCRYPTED_OVERHEAD 35 #define REC_MAX_CIPHER_TEXT_LEN (REC_MAX_PLAIN_LENGTH + REC_MAX_ENCRYPTED_OVERHEAD) /* Maximum ciphertext length */ 36 37 #define REC_MAX_AES_GCM_ENCRYPTION_LIMIT 23726566u /* RFC 8446 5.5 Limits on Key Usage AES-GCM SHOULD under 2^24.5 */ 38 39 typedef struct { 40 RecConnState *outdatedState; 41 RecConnState *currentState; 42 RecConnState *pendingState; 43 } RecConnStates; 44 45 typedef int32_t (*REC_ReadFunc)(TLS_Ctx *, REC_Type, uint8_t *, uint32_t *, uint32_t); 46 typedef int32_t (*REC_WriteFunc)(TLS_Ctx *, REC_Type, const uint8_t *, uint32_t); 47 typedef struct { 48 ListHead head; /* Linked list header */ 49 bool isExistCcsMsg; /* Check whether CCS messages exist in the retransmission message queue */ 50 REC_Type type; /* message type */ 51 uint8_t *msg; /* message data */ 52 uint32_t len; /* message length */ 53 } RecRetransmitList; 54 55 typedef struct RecCtx { 56 RecBuf *inBuf; /* Buffer for reading data */ 57 RecBuf *outBuf; /* Buffer for writing data */ 58 RecConnStates readStates; 59 RecConnStates writeStates; 60 RecBufList *hsRecList; /* hs plaintext data cache */ 61 RecBufList *appRecList; /* app plaintext data cache */ 62 uint32_t emptyRecordCnt; /* Count of empty records */ 63 #ifdef HITLS_TLS_PROTO_DTLS12 64 uint16_t writeEpoch; 65 uint16_t readEpoch; 66 67 RecRetransmitList retransmitList; /* Cache the messages that may be retransmitted during the handshake */ 68 69 /* Process out-of-order messages */ 70 UnprocessedHsMsg unprocessedHsMsg; /* used to cache out-of-order finished messages */ 71 /* unprocessed app message: app messages received in the CCS and finished receiving phases */ 72 UnprocessedAppMsg unprocessedAppMsgList; 73 #endif 74 REC_ReadFunc recRead; 75 void *rUserData; 76 REC_WriteFunc recWrite; 77 void *wUserData; 78 REC_Type unexpectedMsgType; 79 uint32_t pendingDataSize; /* Data length */ 80 const uint8_t *pendingData; /* Plain Data content */ 81 } RecCtx; 82 83 84 /** 85 * @brief Obtain the size of the buffer for read and write operations 86 * 87 * @param ctx [IN] TLS_Ctx context 88 * @param isRead [IN] is read buffer 89 * 90 * @retval HITLS_SUCCESS 91 * @retval HITLS_INTERNAL_EXCEPTION Access a null pointer 92 */ 93 uint32_t RecGetInitBufferSize(const TLS_Ctx *ctx, bool isRead); 94 95 int32_t RecDerefBufList(TLS_Ctx *ctx); 96 97 void RecClearAlertCount(TLS_Ctx *ctx, REC_Type recordType); 98 99 #ifdef __cplusplus 100 } 101 #endif 102 103 #endif /* RECORD_H */ 104