aboutsummaryrefslogtreecommitdiffstats
path: root/src/ssl/hello.cpp
blob: ba30ec5f71acb059d2d0f7452b4b00ce98054125 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* TLS Hello Messages
* (C) 2004-2006 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/tls_messages.h>
#include <botan/tls_exceptn.h>
#include <botan/loadstor.h>
#include <botan/exceptn.h>

namespace Botan {

/**
* Encode and send a Handshake message
*/
void HandshakeMessage::send(Record_Writer& writer, HandshakeHash& hash) const
   {
   SecureVector<byte> buf = serialize();
   SecureVector<byte> send_buf(4);

   u32bit buf_size = buf.size();

   send_buf[0] = type();
   send_buf[1] = get_byte(1, buf_size);
   send_buf[2] = get_byte(2, buf_size);
   send_buf[3] = get_byte(3, buf_size);

   send_buf.append(buf);

   hash.update(send_buf);

   writer.send(HANDSHAKE, send_buf, send_buf.size());
   writer.flush();
   }

/**
* Create a new Hello Request message
*/
Hello_Request::Hello_Request(Record_Writer& writer)
   {
   HandshakeHash dummy; // FIXME: *UGLY*
   send(writer, dummy);
   }

/**
* Serialize a Hello Request message
*/
SecureVector<byte> Hello_Request::serialize() const
   {
   return SecureVector<byte>();
   }

/**
* Deserialize a Hello Request message
*/
void Hello_Request::deserialize(const MemoryRegion<byte>& buf)
   {
   if(buf.size())
      throw Decoding_Error("Hello_Request: Must be empty, and is not");
   }

/**
* Create a new Client Hello message
*/
Client_Hello::Client_Hello(RandomNumberGenerator& rng,
                           Record_Writer& writer, const Policy* policy,
                           HandshakeHash& hash)
   {
   c_random.resize(32);
   rng.randomize(c_random, c_random.size());

   suites = policy->ciphersuites();
   comp_algos = policy->compression();
   c_version = policy->pref_version();

   send(writer, hash);
   }

/**
* Serialize a Client Hello message
*/
SecureVector<byte> Client_Hello::serialize() const
   {
   SecureVector<byte> buf;

   buf.append(static_cast<byte>(c_version >> 8));
   buf.append(static_cast<byte>(c_version     ));
   buf.append(c_random);
   buf.append(static_cast<byte>(sess_id.size()));
   buf.append(sess_id);

   u16bit suites_size = 2*suites.size();

   buf.append(get_byte(0, suites_size));
   buf.append(get_byte(1, suites_size));
   for(u32bit j = 0; j != suites.size(); j++)
      {
      buf.append(get_byte(0, suites[j]));
      buf.append(get_byte(1, suites[j]));
      }

   buf.append(static_cast<byte>(comp_algos.size()));
   for(u32bit j = 0; j != comp_algos.size(); j++)
      buf.append(comp_algos[j]);

   return buf;
   }

/**
* Deserialize a Client Hello message
*/
void Client_Hello::deserialize(const MemoryRegion<byte>& buf)
   {
   if(buf.size() == 0)
      throw Decoding_Error("Client_Hello: Packet corrupted");

   if(buf.size() < 41)
      throw Decoding_Error("Client_Hello: Packet corrupted");

   c_version = static_cast<Version_Code>(make_u16bit(buf[0], buf[1]));
   if(c_version != SSL_V3 && c_version != TLS_V10)
      throw TLS_Exception(PROTOCOL_VERSION, "Client_Hello: Bad version code");

   c_random.set(buf + 2, 32);

   u32bit session_id_len = buf[34];
   if(session_id_len > 32 || session_id_len + 41 > buf.size())
      throw Decoding_Error("Client_Hello: Packet corrupted");
   sess_id.copy(buf + 35, session_id_len);

   u32bit offset = 2+32+1+session_id_len;

   u16bit suites_size = make_u16bit(buf[offset], buf[offset+1]);
   offset += 2;
   if(suites_size % 2 == 1 || offset + suites_size + 2 > buf.size())
      throw Decoding_Error("Client_Hello: Packet corrupted");

   for(u32bit j = 0; j != suites_size; j += 2)
      {
      u16bit suite = make_u16bit(buf[offset+j], buf[offset+j+1]);
      suites.push_back(suite);
      }
   offset += suites_size;

   byte comp_algo_size = buf[offset];
   offset += 1;
   if(offset + comp_algo_size > buf.size())
      throw Decoding_Error("Client_Hello: Packet corrupted");

   for(u32bit j = 0; j != comp_algo_size; j++)
      comp_algos.push_back(buf[offset+j]);
   }

/**
* Check if we offered this ciphersuite
*/
bool Client_Hello::offered_suite(u16bit ciphersuite) const
   {
   for(u32bit j = 0; j != suites.size(); j++)
      if(suites[j] == ciphersuite)
         return true;
   return false;
   }

/**
* Create a new Server Hello message
*/
Server_Hello::Server_Hello(RandomNumberGenerator& rng,
                           Record_Writer& writer, const Policy* policy,
                           const std::vector<X509_Certificate>& certs,
                           const Client_Hello& c_hello, Version_Code ver,
                           HandshakeHash& hash)
   {
   bool have_rsa = false, have_dsa = false;
   for(u32bit j = 0; j != certs.size(); j++)
      {
      X509_PublicKey* key = certs[j].subject_public_key();
      if(key->algo_name() == "RSA") have_rsa = true;
      if(key->algo_name() == "DSA") have_dsa = true;
      }

   suite = policy->choose_suite(c_hello.ciphersuites(), have_rsa, have_dsa);
   comp_algo = policy->choose_compression(c_hello.compression_algos());

   s_version = ver;
   s_random.resize(32);
   rng.randomize(s_random, s_random.size());

   send(writer, hash);
   }

/**
* Serialize a Server Hello message
*/
SecureVector<byte> Server_Hello::serialize() const
   {
   SecureVector<byte> buf;

   buf.append(static_cast<byte>(s_version >> 8));
   buf.append(static_cast<byte>(s_version     ));
   buf.append(s_random);
   buf.append(static_cast<byte>(sess_id.size()));
   buf.append(sess_id);

   buf.append(get_byte(0, suite));
   buf.append(get_byte(1, suite));

   buf.append(comp_algo);

   return buf;
   }

/**
* Deserialize a Server Hello message
*/
void Server_Hello::deserialize(const MemoryRegion<byte>& buf)
   {
   if(buf.size() < 38)
      throw Decoding_Error("Server_Hello: Packet corrupted");

   s_version = static_cast<Version_Code>(make_u16bit(buf[0], buf[1]));
   if(s_version != SSL_V3 && s_version != TLS_V10)
      throw TLS_Exception(PROTOCOL_VERSION,
                          "Server_Hello: Unsupported server version");

   s_random.set(buf + 2, 32);

   u32bit session_id_len = buf[2+32];
   if(session_id_len > 32 || session_id_len + 38 != buf.size())
      throw Decoding_Error("Server_Hello: Packet corrupted");
   sess_id.copy(buf + 2 + 32 + 1, session_id_len);

   suite = make_u16bit(buf[2+32+1+session_id_len],
                       buf[2+32+1+session_id_len+1]);
   comp_algo = buf[2+32+1+session_id_len+2];
   }


/**
* Create a new Server Hello Done message
*/
Server_Hello_Done::Server_Hello_Done(Record_Writer& writer,
                                     HandshakeHash& hash)
   {
   send(writer, hash);
   }

/**
* Serialize a Server Hello Done message
*/
SecureVector<byte> Server_Hello_Done::serialize() const
   {
   return SecureVector<byte>();
   }

/**
* Deserialize a Server Hello Done message
*/
void Server_Hello_Done::deserialize(const MemoryRegion<byte>& buf)
   {
   if(buf.size())
      throw Decoding_Error("Server_Hello_Done: Must be empty, and is not");
   }

}