Skip to content

Commit

Permalink
Merge pull request #23 from me-no-dev/add-send-calculator
Browse files Browse the repository at this point in the history
add send packet size calculator
  • Loading branch information
igrr committed Aug 29, 2016
2 parents d26f23a + 23d532a commit 144994c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ssl/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
29 changes: 29 additions & 0 deletions ssl/tls1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 144994c

Please sign in to comment.