Botan 1.10.17

Public Member Functions | List of all members
Botan::Record_Writer Class Reference

`#include <tls_record.h>`

## Public Member Functions
void | alert (Alert_Level, Alert_Type)
void | flush ()
| Record_Writer (std::tr1::function< void(const byte[], size_t)> output_fn)
void | reset ()
void | send (byte type, const byte input[], size_t length)
void | send (byte type, byte val)
void | set_keys (const CipherSuite &, const SessionKeys &, Connection_Side)
void | set_version (Version_Code)
| ~Record_Writer ()

## Detailed Description

TLS Record Writer

Definition at line 39 of file tls_record.h.

## Constructor & Destructor Documentation

## ◆ Record_Writer()

Botan::Record_Writer::Record_Writer | ( | std::tr1::function< void(const byte[], size_t)> | out | ) |

Record_Writer Constructor

Definition at line 19 of file rec_wri.cpp.

References reset().

19 :
20 output_fn(out),
21 buffer(DEFAULT_BUFFERSIZE)
22 {
23 mac = 0;
24 reset();
25 }
Botan::Record_Writer::reset
void reset()
Definition: rec_wri.cpp:30

## ◆ ~Record_Writer()

Botan::Record_Writer::~Record_Writer | ( |  | ) |
| inline

Definition at line 57 of file tls_record.h.

57 { delete mac; }

## Member Function Documentation

## ◆ alert()

void Botan::Record_Writer::alert | ( | Alert_Level | level,
|  | Alert_Type | type
| ) |  |

Send an alert

Definition at line 264 of file rec_wri.cpp.

References Botan::ALERT, flush(), and send().

265 {
266 byte alert[2] = { level, type };
267 send(ALERT, alert, sizeof(alert));
268 flush();
269 }
Botan::ALERT
Definition: tls_magic.h:35
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Record_Writer::flush
void flush()
Definition: rec_wri.cpp:162
Botan::Record_Writer::alert
void alert(Alert_Level, Alert_Type)
Definition: rec_wri.cpp:264
Botan::Record_Writer::send
void send(byte type, const byte input[], size_t length)
Definition: rec_wri.cpp:131

## ◆ flush()

void Botan::Record_Writer::flush | ( |  | ) |

Split buffer into records, and send them all

Definition at line 162 of file rec_wri.cpp.

References Botan::MAX_PLAINTEXT_SIZE.

Referenced by alert(), Botan::TLS_Server::read(), Botan::TLS_Client::read(), Botan::HandshakeMessage::send(), and send().

163 {
164 const byte* buf_ptr = &buffer[0];
165 size_t offset = 0;
166
167 while(offset != buf_pos)
168 {
169 size_t record_size = buf_pos - offset;
170 if(record_size > MAX_PLAINTEXT_SIZE)
171 record_size = MAX_PLAINTEXT_SIZE;
172
173 send_record(buf_type, buf_ptr + offset, record_size);
174 offset += record_size;
175 }
176 buf_type = 0;
177 buf_pos = 0;
178 }
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::MAX_PLAINTEXT_SIZE
Definition: tls_magic.h:17

## ◆ reset()

void Botan::Record_Writer::reset | ( |  | ) |

Reset the state

Definition at line 30 of file rec_wri.cpp.

References Botan::Pipe::reset(), and Botan::zeroise().

Referenced by Record_Writer().

31 {
32 cipher.reset();
33
34 delete mac;
35 mac = 0;
36
37 zeroise(buffer);
38 buf_pos = 0;
39
40 major = minor = buf_type = 0;
41 block_size = 0;
42 mac_size = 0;
43 iv_size = 0;
44
45 seq_no = 0;
46 }
Botan::Pipe::reset
void reset()
Definition: pipe.cpp:76
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428

## ◆ send() [1/2]

void Botan::Record_Writer::send | ( | byte | type,
|  | const byte | input[],
|  | size_t | length
| ) |  |

Send one or more records to the other side

Definition at line 131 of file rec_wri.cpp.

References Botan::MemoryRegion< T >::copy(), flush(), and Botan::MemoryRegion< T >::size().

Referenced by alert(), Botan::HandshakeMessage::send(), Botan::TLS_Server::write(), and Botan::TLS_Client::write().

132 {
133 if(type != buf_type)
134 flush();
135
136 const size_t BUFFER_SIZE = buffer.size();
137 buf_type = type;
138
139 // FIXME: compression right here
140
141 buffer.copy(buf_pos, input, length);
142 if(buf_pos + length >= BUFFER_SIZE)
143 {
144 send_record(buf_type, &buffer[0], length);
145 input += (BUFFER_SIZE - buf_pos);
146 length -= (BUFFER_SIZE - buf_pos);
147 while(length >= BUFFER_SIZE)
148 {
149 send_record(buf_type, input, BUFFER_SIZE);
150 input += BUFFER_SIZE;
151 length -= BUFFER_SIZE;
152 }
153 buffer.copy(input, length);
154 buf_pos = 0;
155 }
156 buf_pos += length;
157 }
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:126
Botan::Record_Writer::flush
void flush()
Definition: rec_wri.cpp:162
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ send() [2/2]

void Botan::Record_Writer::send | ( | byte | type,
|  | byte | val
| ) |  |
| inline

Definition at line 43 of file tls_record.h.

References send().

Referenced by send().

43 { send(type, &val, 1); }
Botan::Record_Writer::send
void send(byte type, const byte input[], size_t length)
Definition: rec_wri.cpp:131

## ◆ set_keys()

void Botan::Record_Writer::set_keys | ( | const CipherSuite & | suite,
|  | const SessionKeys & | keys,
|  | Connection_Side | side
| ) |  |

Set the keys for writing

Definition at line 63 of file rec_wri.cpp.

References Botan::Library_State::algorithm_factory(), Botan::Pipe::append(), Botan::block_size_of(), Botan::CipherSuite::cipher_algo(), Botan::CLIENT, Botan::SessionKeys::client_cipher_key(), Botan::SessionKeys::client_iv(), Botan::SessionKeys::client_mac_key(), Botan::ENCRYPTION, Botan::get_cipher(), Botan::Global_State_Management::global_state(), Botan::have_block_cipher(), Botan::have_hash(), Botan::have_stream_cipher(), Botan::CipherSuite::mac_algo(), Botan::Algorithm_Factory::make_mac(), Botan::Buffered_Computation::output_length(), Botan::Pipe::reset(), Botan::SessionKeys::server_cipher_key(), Botan::SessionKeys::server_iv(), Botan::SessionKeys::server_mac_key(), and Botan::SymmetricAlgorithm::set_key().

65 {
66 cipher.reset();
67 delete mac;
68 mac = 0;
69 seq_no = 0;
70
71 SymmetricKey mac_key, cipher_key;
72 InitializationVector iv;
73
74 if(side == CLIENT)
75 {
76 cipher_key = keys.client_cipher_key();
77 iv = keys.client_iv();
78 mac_key = keys.client_mac_key();
79 }
80 else
81 {
82 cipher_key = keys.server_cipher_key();
83 iv = keys.server_iv();
84 mac_key = keys.server_mac_key();
85 }
86
87 const std::string cipher_algo = suite.cipher_algo();
88 const std::string mac_algo = suite.mac_algo();
89
90 if(have_block_cipher(cipher_algo))
91 {
92 cipher.append(get_cipher(
93 cipher_algo + "/CBC/NoPadding",
94 cipher_key, iv, ENCRYPTION)
95 );
96 block_size = block_size_of(cipher_algo);
97
98 if(major > 3 || (major == 3 && minor >= 2))
99 iv_size = block_size;
100 else
101 iv_size = 0;
102 }
103 else if(have_stream_cipher(cipher_algo))
104 {
105 cipher.append(get_cipher(cipher_algo, cipher_key, ENCRYPTION));
106 block_size = 0;
107 iv_size = 0;
108 }
109 else
110 throw Invalid_Argument("Record_Writer: Unknown cipher " + cipher_algo);
111
112 if(have_hash(mac_algo))
113 {
114 Algorithm_Factory& af = global_state().algorithm_factory();
115
116 if(major == 3 && minor == 0)
117 mac = af.make_mac("SSL3-MAC(" + mac_algo + ")");
118 else
119 mac = af.make_mac("HMAC(" + mac_algo + ")");
120
121 mac->set_key(mac_key);
122 mac_size = mac->output_length();
123 }
124 else
125 throw Invalid_Argument("Record_Writer: Unknown hash " + mac_algo);
126 }
Botan::Pipe::append
void append(Filter *filt)
Definition: pipe.cpp:215
Botan::Library_State::algorithm_factory
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
Botan::ENCRYPTION
Definition: sym_algo.h:87
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::Pipe::reset
void reset()
Definition: pipe.cpp:76
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:147
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::block_size_of
size_t block_size_of(const std::string &name)
Definition: lookup.cpp:35
Botan::have_stream_cipher
bool have_stream_cipher(const std::string &algo_spec)
Definition: lookup.h:248
Botan::have_hash
bool have_hash(const std::string &algo_spec)
Definition: lookup.h:261
Botan::have_block_cipher
bool have_block_cipher(const std::string &algo_spec)
Definition: lookup.h:235
Botan::get_cipher
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: lookup.cpp:124
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0
Botan::CLIENT
Definition: tls_magic.h:29
Botan::InitializationVector
OctetString InitializationVector
Definition: symkey.h:152

## ◆ set_version()

void Botan::Record_Writer::set_version | ( | Version_Code | version | ) |

Set the version to use

Definition at line 51 of file rec_wri.cpp.

References Botan::SSL_V3, Botan::TLS_V10, and Botan::TLS_V11.

52 {
53 if(version != SSL_V3 && version != TLS_V10 && version != TLS_V11)
54 throw Invalid_Argument("Record_Writer: Invalid protocol version");
55
56 major = (version >> 8) & 0xFF;
57 minor = (version & 0xFF);
58 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::TLS_V10
Definition: tls_magic.h:25
Botan::SSL_V3
Definition: tls_magic.h:24
Botan::TLS_V11
Definition: tls_magic.h:26

The documentation for this class was generated from the following files:

- src/ssl/tls_record.h
- src/ssl/rec_wri.cpp