aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_handshake_io.cpp
blob: f46fdb09489eb5870a2dc06913f09cb3e4b4b728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* TLS Handshake IO
* (C) 2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/internal/tls_handshake_io.h>
#include <botan/internal/tls_messages.h>
#include <botan/tls_record.h>
#include <botan/exceptn.h>

namespace Botan {

namespace TLS {

namespace {

inline size_t load_be24(const byte q[3])
   {
   return make_u32bit(0,
                      q[0],
                      q[1],
                      q[2]);
   }

void store_be24(byte out[3], size_t val)
   {
   out[0] = get_byte<u32bit>(1, val);
   out[1] = get_byte<u32bit>(2, val);
   out[2] = get_byte<u32bit>(3, val);
   }

}

Protocol_Version Stream_Handshake_IO::initial_record_version() const
   {
   return Protocol_Version::SSL_V3;
   }

void Stream_Handshake_IO::add_input(const byte rec_type,
                                    const byte record[],
                                    size_t record_size,
                                    u64bit /*record_number*/)
   {
   if(rec_type == HANDSHAKE)
      {
      m_queue.insert(m_queue.end(), record, record + record_size);
      }
   else if(rec_type == CHANGE_CIPHER_SPEC)
      {
      if(record_size != 1 || record[0] != 1)
         throw Decoding_Error("Invalid ChangeCipherSpec");

      // Pretend it's a regular handshake message of zero length
      const byte ccs_hs[] = { HANDSHAKE_CCS, 0, 0, 0 };
      m_queue.insert(m_queue.end(), ccs_hs, ccs_hs + sizeof(ccs_hs));
      }
   else
      throw Decoding_Error("Unknown message type in handshake processing");
   }

std::pair<Handshake_Type, std::vector<byte>>
Stream_Handshake_IO::get_next_record()
   {
   if(m_queue.size() >= 4)
      {
      const size_t length = load_be24(&m_queue[1]);

      if(m_queue.size() >= length + 4)
         {
         Handshake_Type type = static_cast<Handshake_Type>(m_queue[0]);

         std::vector<byte> contents(m_queue.begin() + 4,
                                    m_queue.begin() + 4 + length);

         m_queue.erase(m_queue.begin(), m_queue.begin() + 4 + length);

         return std::make_pair(type, contents);
         }
      }

   return std::make_pair(HANDSHAKE_NONE, std::vector<byte>());
   }

std::vector<byte>
Stream_Handshake_IO::format(const std::vector<byte>& msg,
                            Handshake_Type type)
   {
   std::vector<byte> send_buf(4 + msg.size());

   const size_t buf_size = msg.size();

   send_buf[0] = type;

   store_be24(&send_buf[1], buf_size);

   copy_mem(&send_buf[4], &msg[0], msg.size());

   return send_buf;
   }

std::vector<byte> Stream_Handshake_IO::send(Handshake_Message& msg)
   {
   const std::vector<byte> buf = format(msg.serialize(), msg.type());

   m_writer.send(HANDSHAKE, &buf[0], buf.size());

   return buf;
   }

Protocol_Version Datagram_Handshake_IO::initial_record_version() const
   {
   return Protocol_Version::DTLS_V10;
   }

void Datagram_Handshake_IO::add_input(const byte rec_type,
                                      const byte record[],
                                      size_t record_size,
                                      u64bit record_number)
   {
   if(rec_type == CHANGE_CIPHER_SPEC)
      {
      m_ccs_epochs.insert(static_cast<u16bit>(record_number >> 48));
      return;
      }

   const size_t DTLS_HANDSHAKE_HEADER_LEN = 12;

   if(record_size < DTLS_HANDSHAKE_HEADER_LEN)
      return; // completely bogus? at least degenerate/weird

   const byte msg_type = record[0];
   const size_t msg_len = load_be24(&record[1]);
   const u16bit message_seq = load_be<u16bit>(&record[4], 0);
   const size_t fragment_offset = load_be24(&record[6]);
   const size_t fragment_length = load_be24(&record[9]);

   if(fragment_length + DTLS_HANDSHAKE_HEADER_LEN != record_size)
      throw Decoding_Error("Bogus DTLS handshake, header sizes do not match");

   m_messages[message_seq].add_fragment(&record[DTLS_HANDSHAKE_HEADER_LEN],
                                        fragment_length,
                                        fragment_offset,
                                        msg_type,
                                        msg_len);
   }

std::pair<Handshake_Type, std::vector<byte>>
Datagram_Handshake_IO::get_next_record()
   {
   auto i = m_messages.find(m_in_message_seq);

   if(i == m_messages.end() || !i->second.complete())
      return std::make_pair(HANDSHAKE_NONE, std::vector<byte>());

   auto m = i->second.message();

   m_in_message_seq += 1;

   return m;
   //return i->second.message();
   }

void Datagram_Handshake_IO::Handshake_Reassembly::add_fragment(
   const byte fragment[],
   size_t fragment_length,
   size_t fragment_offset,
   byte msg_type,
   size_t msg_length)
   {
   if(m_msg_type == HANDSHAKE_NONE)
      {
      m_msg_type = msg_type;
      m_msg_length = msg_length;
#warning DoS should resize as inputs are added (?)
      m_buffer.resize(m_msg_length);
      }

   if(msg_type != m_msg_type || msg_length != m_msg_length)
      throw Decoding_Error("Datagram_Handshake_IO - inconsistent values");

   copy_mem(&m_buffer[fragment_offset], fragment, fragment_length);
   }

bool Datagram_Handshake_IO::Handshake_Reassembly::complete() const
   {
   return true; // FIXME
   }

std::pair<Handshake_Type, std::vector<byte>>
Datagram_Handshake_IO::Handshake_Reassembly::message() const
   {
   if(!complete())
      throw Internal_Error("Datagram_Handshake_IO - message not complete");

   auto msg = std::make_pair(static_cast<Handshake_Type>(m_msg_type), m_buffer);

   return msg;
   }

std::vector<byte>
Datagram_Handshake_IO::format(const std::vector<byte>& msg,
                              Handshake_Type type)
   {
   std::vector<byte> send_buf(12 + msg.size());

   const size_t buf_size = msg.size();

   send_buf[0] = type;

   store_be24(&send_buf[1], buf_size);

   store_be(static_cast<u16bit>(m_in_message_seq - 1), &send_buf[4]);

   store_be24(&send_buf[6], 0); // fragment_offset
   store_be24(&send_buf[9], buf_size); // fragment_length

   copy_mem(&send_buf[12], &msg[0], msg.size());

   return send_buf;
   }

std::vector<byte>
Datagram_Handshake_IO::send(Handshake_Message& msg)
   {
   const std::vector<byte> buf = format(msg.serialize(), msg.type());

   // FIXME: fragment to mtu size
   m_writer.send(HANDSHAKE, &buf[0], buf.size());

   return buf;

   }

}

}