diff options
author | lloyd <[email protected]> | 2012-09-06 19:29:25 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2012-09-06 19:29:25 +0000 |
commit | 42a230bd58c3005515e19a9c6d88b24a882b984d (patch) | |
tree | 8364e2a5ef0b4a70380a1fdbbd1d234989f96e0c /src | |
parent | 3c72c80e15d93c03f87356fdcadc43b7be444701 (diff) |
Have write_record resize the vector as it goes, thus the return value
is not needed. Doesn't actually cause reallocations as we preallocate.
Diffstat (limited to 'src')
-rw-r--r-- | src/tls/tls_channel.cpp | 18 | ||||
-rw-r--r-- | src/tls/tls_record.cpp | 62 | ||||
-rw-r--r-- | src/tls/tls_record.h | 12 |
3 files changed, 41 insertions, 51 deletions
diff --git a/src/tls/tls_channel.cpp b/src/tls/tls_channel.cpp index 4696a49d5..5e1e546a4 100644 --- a/src/tls/tls_channel.cpp +++ b/src/tls/tls_channel.cpp @@ -365,17 +365,17 @@ void Channel::write_record(byte record_type, const byte input[], size_t length) record_version = m_state->handshake_io().initial_record_version(); } - const size_t written = TLS::write_record(m_writebuf, - record_type, - input, - length, - m_write_seq_no, - record_version, - m_write_cipherstate.get(), - m_rng); + TLS::write_record(m_writebuf, + record_type, + input, + length, + m_write_seq_no, + record_version, + m_write_cipherstate.get(), + m_rng); m_write_seq_no += 1; - m_output_fn(&m_writebuf[0], written); + m_output_fn(&m_writebuf[0], m_writebuf.size()); } void Channel::send(const byte buf[], size_t buf_size) diff --git a/src/tls/tls_record.cpp b/src/tls/tls_record.cpp index f2db95bc4..cec2abed2 100644 --- a/src/tls/tls_record.cpp +++ b/src/tls/tls_record.cpp @@ -74,28 +74,27 @@ Connection_Cipher_State::Connection_Cipher_State( m_mac->set_key(mac_key); } -size_t write_record(std::vector<byte>& output, - byte msg_type, const byte msg[], size_t msg_length, - u64bit msg_sequence_number, - Protocol_Version version, - Connection_Cipher_State* cipherstate, - RandomNumberGenerator& rng) +void write_record(std::vector<byte>& output, + byte msg_type, const byte msg[], size_t msg_length, + u64bit msg_sequence_number, + Protocol_Version version, + Connection_Cipher_State* cipherstate, + RandomNumberGenerator& rng) { - BOTAN_ASSERT(output.size() >= TLS_HEADER_SIZE + msg_length, - "Write buffer is big enough"); + output.clear(); - output[0] = msg_type; - output[1] = version.major_version(); - output[2] = version.minor_version(); + output.push_back(msg_type); + output.push_back(version.major_version()); + output.push_back(version.minor_version()); if(!cipherstate) // initial unencrypted handshake records { - output[3] = get_byte<u16bit>(0, msg_length); - output[4] = get_byte<u16bit>(1, msg_length); + output.push_back(get_byte<u16bit>(0, msg_length)); + output.push_back(get_byte<u16bit>(1, msg_length)); - copy_mem(&output[TLS_HEADER_SIZE], msg, msg_length); + output.insert(output.end(), &msg[0], &msg[msg_length]); - return (TLS_HEADER_SIZE + msg_length); + return; } cipherstate->mac()->update_be(msg_sequence_number); @@ -120,27 +119,23 @@ size_t write_record(std::vector<byte>& output, block_size); if(buf_size >= MAX_CIPHERTEXT_SIZE) - throw Internal_Error("Record_Writer: Record is too big"); + throw Internal_Error("Output record is larger than allowed by protocol"); - BOTAN_ASSERT(output.size() >= TLS_HEADER_SIZE + MAX_CIPHERTEXT_SIZE, - "Write buffer is big enough"); + output.push_back(get_byte<u16bit>(0, buf_size)); + output.push_back(get_byte<u16bit>(1, buf_size)); - output[3] = get_byte<u16bit>(0, buf_size); - output[4] = get_byte<u16bit>(1, buf_size); - - byte* buf_write_ptr = &output[TLS_HEADER_SIZE]; + const size_t header_size = output.size(); if(iv_size) { - rng.randomize(buf_write_ptr, iv_size); - buf_write_ptr += iv_size; + output.resize(output.size() + iv_size); + rng.randomize(&output[output.size() - iv_size], iv_size); } - copy_mem(buf_write_ptr, msg, msg_length); - buf_write_ptr += msg_length; + output.insert(output.end(), &msg[0], &msg[msg_length]); - cipherstate->mac()->final(buf_write_ptr); - buf_write_ptr += mac_size; + output.resize(output.size() + mac_size); + cipherstate->mac()->final(&output[output.size() - mac_size]); if(block_size) { @@ -148,10 +143,7 @@ size_t write_record(std::vector<byte>& output, buf_size - (iv_size + msg_length + mac_size + 1); for(size_t i = 0; i != pad_val + 1; ++i) - { - *buf_write_ptr = pad_val; - buf_write_ptr += 1; - } + output.push_back(pad_val); } if(buf_size > MAX_CIPHERTEXT_SIZE) @@ -159,7 +151,7 @@ size_t write_record(std::vector<byte>& output, if(StreamCipher* sc = cipherstate->stream_cipher()) { - sc->cipher1(&output[TLS_HEADER_SIZE], buf_size); + sc->cipher1(&output[header_size], buf_size); } else if(BlockCipher* bc = cipherstate->block_cipher()) { @@ -168,7 +160,7 @@ size_t write_record(std::vector<byte>& output, BOTAN_ASSERT(buf_size % block_size == 0, "Buffer is an even multiple of block size"); - byte* buf = &output[TLS_HEADER_SIZE]; + byte* buf = &output[header_size]; const size_t blocks = buf_size / block_size; @@ -186,8 +178,6 @@ size_t write_record(std::vector<byte>& output, } else throw Internal_Error("NULL cipher not supported"); - - return (TLS_HEADER_SIZE + buf_size); } namespace { diff --git a/src/tls/tls_record.h b/src/tls/tls_record.h index 228e8e4a4..df4858fb9 100644 --- a/src/tls/tls_record.h +++ b/src/tls/tls_record.h @@ -73,12 +73,12 @@ class Connection_Cipher_State * @param rng is a random number generator * @return number of bytes written to write_buffer */ -size_t write_record(std::vector<byte>& write_buffer, - byte msg_type, const byte msg[], size_t msg_length, - u64bit msg_sequence_number, - Protocol_Version version, - Connection_Cipher_State* cipherstate, - RandomNumberGenerator& rng); +void write_record(std::vector<byte>& write_buffer, + byte msg_type, const byte msg[], size_t msg_length, + u64bit msg_sequence_number, + Protocol_Version version, + Connection_Cipher_State* cipherstate, + RandomNumberGenerator& rng); /** * Decode a TLS record |