From 23d532ae2d43f4f1def95e4c02307c90ad5fe50f Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Sat, 27 Aug 2016 18:47:02 +0300 Subject: [PATCH] add send packet size calculator --- ssl/ssl.h | 9 +++++++++ ssl/tls1.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/ssl/ssl.h b/ssl/ssl.h index fb91602f9c..9bf91b39b1 100644 --- a/ssl/ssl.h +++ b/ssl/ssl.h @@ -296,6 +296,15 @@ EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data); */ EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len); +/** + * @brief Calculate the size of the encrypted data from what you are about to send + * @param ssl [in] An SSL obect reference. + * @param out_len [in] The number of bytes to be written. + * @return The number of bytes that will be sent, or if < 0 if an error. + * @see ssl.h for the error code list. + */ +EXP_FUNC int STDCALL ssl_calculate_write_length(SSL *ssl, int out_len); + /** * @brief Find an ssl object based on a file descriptor. * diff --git a/ssl/tls1.c b/ssl/tls1.c index c9615de1cd..ccb253a9bf 100644 --- a/ssl/tls1.c +++ b/ssl/tls1.c @@ -315,6 +315,35 @@ EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len) return out_len; } +EXP_FUNC int STDCALL ssl_calculate_write_length(SSL *ssl, int length) +{ + int msg_length = 0; + if (ssl->hs_status == SSL_ERROR_DEAD) + return SSL_ERROR_CONN_LOST; + + if (ssl->flag & SSL_SENT_CLOSE_NOTIFY) + return SSL_CLOSE_NOTIFY; + + msg_length += length; + + if (ssl->flag & SSL_TX_ENCRYPTED) + { + msg_length += ssl->cipher_info->digest_size; + { + int last_blk_size = msg_length%ssl->cipher_info->padding_size; + int pad_bytes = ssl->cipher_info->padding_size - last_blk_size; + if (pad_bytes == 0) + pad_bytes += ssl->cipher_info->padding_size; + msg_length += pad_bytes; + } + if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_1) + { + msg_length += ssl->cipher_info->iv_size; + } + } + return SSL_RECORD_SIZE+msg_length; +} + /** * Add a certificate to the certificate chain. */