Botan 1.10.17
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, byte val)
void send (byte type, const byte input[], size_t length)
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.

19 :
20 output_fn(out),
21 buffer(DEFAULT_BUFFERSIZE)
22 {
23 mac = 0;
24 reset();
25 }

References reset().

◆ ~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.

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

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

Referenced by alert().

◆ flush()

void Botan::Record_Writer::flush ( )

Split buffer into records, and send them all

Definition at line 162 of file rec_wri.cpp.

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 }
@ MAX_PLAINTEXT_SIZE
Definition tls_magic.h:17

References Botan::MAX_PLAINTEXT_SIZE.

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

◆ reset()

void Botan::Record_Writer::reset ( )

Reset the state

Definition at line 30 of file rec_wri.cpp.

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 }
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428

References Botan::zeroise().

Referenced by Record_Writer().

◆ send() [1/2]

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

Definition at line 43 of file tls_record.h.

43{ send(type, &val, 1); }

References send().

Referenced by send().

◆ send() [2/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.

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 }

References flush().

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

◆ 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.

65 {
66 cipher.reset();
67 delete mac;
68 mac = 0;
69 seq_no = 0;
70
71 SymmetricKey mac_key, cipher_key;
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 }
MessageAuthenticationCode * make_mac(const std::string &algo_spec, const std::string &provider="")
Algorithm_Factory & algorithm_factory() const
Definition libstate.cpp:173
void set_key(const SymmetricKey &key)
Definition sym_algo.h:60
OctetString SymmetricKey
Definition symkey.h:147
bool have_stream_cipher(const std::string &algo_spec)
Definition lookup.h:248
bool have_hash(const std::string &algo_spec)
Definition lookup.h:261
@ CLIENT
Definition tls_magic.h:29
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition lookup.cpp:124
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
Library_State & global_state()
OctetString InitializationVector
Definition symkey.h:152
@ ENCRYPTION
Definition sym_algo.h:87
size_t block_size_of(const std::string &name)
Definition lookup.cpp:35
bool have_block_cipher(const std::string &algo_spec)
Definition lookup.h:235

References Botan::Library_State::algorithm_factory(), 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(), Botan::have_block_cipher(), Botan::have_hash(), Botan::have_stream_cipher(), Botan::CipherSuite::mac_algo(), Botan::Algorithm_Factory::make_mac(), Botan::SessionKeys::server_cipher_key(), Botan::SessionKeys::server_iv(), Botan::SessionKeys::server_mac_key(), and Botan::SymmetricAlgorithm::set_key().

◆ 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.

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 }
@ SSL_V3
Definition tls_magic.h:24
@ TLS_V10
Definition tls_magic.h:25
@ TLS_V11
Definition tls_magic.h:26

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


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