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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/*
* Copyright (C) 2000,2001 Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* GNUTLS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNUTLS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "gnutls_int.h"
#include "gnutls_handshake.h"
#include "gnutls_kx.h"
#include "gnutls_dh.h"
#include "gnutls_errors.h"
#include "gnutls_algorithms.h"
#include "debug.h"
#include "gnutls_gcry.h"
#define MASTER_SECRET "master secret"
static int generate_normal_master( GNUTLS_STATE state);
int _gnutls_generate_master( GNUTLS_STATE state) {
if (state->gnutls_internals.resumed==RESUME_FALSE)
return generate_normal_master(state);
return 0;
}
static int generate_normal_master( GNUTLS_STATE state) {
int premaster_size;
#ifdef HARD_DEBUG
int i;
#endif
opaque* premaster, *master;
int ret = 0;
char random[2*TLS_RANDOM_SIZE];
memcpy(random, state->security_parameters.client_random, TLS_RANDOM_SIZE);
memcpy(&random[TLS_RANDOM_SIZE], state->security_parameters.server_random, TLS_RANDOM_SIZE);
/* generate premaster */
premaster_size = state->gnutls_key->key.size;
premaster = state->gnutls_key->key.data;
#ifdef HARD_DEBUG
_gnutls_log( "PREMASTER SECRET[%d]: %s\n", premaster_size, _gnutls_bin2hex(premaster, premaster_size));
_gnutls_log( "CLIENT RANDOM[%d]: %s\n", 32, _gnutls_bin2hex(state->security_parameters.client_random,32));
_gnutls_log( "SERVER RANDOM[%d]: %s\n", 32, _gnutls_bin2hex(state->security_parameters.server_random,32));
#endif
if (_gnutls_version_ssl3(state->connection_state.version) == 0) {
master =
gnutls_ssl3_generate_random( premaster, premaster_size,
random, 2*TLS_RANDOM_SIZE, TLS_MASTER_SIZE);
} else {
master =
gnutls_PRF( premaster, premaster_size,
MASTER_SECRET, strlen(MASTER_SECRET),
random, 2*TLS_RANDOM_SIZE, TLS_MASTER_SIZE);
}
secure_free(premaster);
state->gnutls_key->key.size = 0;
state->gnutls_key->key.data = NULL;
if (master==NULL) return GNUTLS_E_MEMORY_ERROR;
#ifdef HARD_DEBUG
_gnutls_log( "MASTER SECRET: %s\n", _gnutls_bin2hex(master, TLS_MASTER_SIZE));
#endif
memcpy(state->security_parameters.master_secret, master, TLS_MASTER_SIZE);
secure_free(master);
return ret;
}
/* This is called when we want to receive the key exchange message of the
* server. It does nothing if this type of message is not required
* by the selected ciphersuite.
*/
int _gnutls_send_server_kx_message(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data = NULL;
int data_size = 0;
int ret = 0;
if (state->gnutls_internals.auth_struct->gnutls_generate_server_kx==NULL)
return 0;
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Sending server KX message\n");
#endif
data_size = state->gnutls_internals.auth_struct->gnutls_generate_server_kx( state->gnutls_key, &data);
if (data_size < 0) {
gnutls_assert();
return data_size;
}
ret = _gnutls_send_handshake(cd, state, data, data_size, GNUTLS_SERVER_KEY_EXCHANGE);
gnutls_free(data);
if (ret < 0) {
gnutls_assert();
return ret;
}
return data_size;
}
/* Currently only used in SRP */
int _gnutls_send_server_kx_message2(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data = NULL;
int data_size = 0;
int ret = 0;
if (state->gnutls_internals.auth_struct->gnutls_generate_server_kx2 != NULL) {
data_size = state->gnutls_internals.auth_struct->gnutls_generate_server_kx2( state->gnutls_key, &data);
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Sending server KX message2\n");
#endif
if (data_size<0) {
gnutls_assert();
return data_size;
}
ret = _gnutls_send_handshake(cd, state, data, data_size, GNUTLS_SERVER_KEY_EXCHANGE);
gnutls_free(data);
if (ret<0) {
gnutls_assert();
return ret;
}
}
return data_size;
}
/* This is the function for the client to send the key
* exchange message
*/
int _gnutls_send_client_kx_message(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data;
int data_size;
int ret = 0;
if (state->gnutls_internals.auth_struct->gnutls_generate_client_kx==NULL)
return 0;
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Sending client KX message\n");
#endif
data_size = state->gnutls_internals.auth_struct->gnutls_generate_client_kx( state->gnutls_key, &data);
if (data_size < 0) {
gnutls_assert();
return data_size;
}
ret = _gnutls_send_handshake(cd, state, data, data_size, GNUTLS_CLIENT_KEY_EXCHANGE);
gnutls_free(data);
if (ret<0) {
gnutls_assert();
return ret;
}
return ret;
}
/* Only used in SRP currently
*/
int _gnutls_send_client_kx_message0(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data;
int data_size;
int ret = 0;
if ( state->gnutls_internals.auth_struct->gnutls_generate_client_kx0 == NULL)
return 0;
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Sending client KX message0\n");
#endif
data_size = state->gnutls_internals.auth_struct->gnutls_generate_client_kx0( state->gnutls_key, &data);
if (data_size < 0) {
gnutls_assert();
return data_size;
}
ret = _gnutls_send_handshake(cd, state, data, data_size, GNUTLS_CLIENT_KEY_EXCHANGE);
gnutls_free(data);
return ret;
}
/* This is the function for the client to send the certificate
* verify message
* FIXME: this function does almost nothing except sending garbage to
* peer.
*/
int _gnutls_send_client_certificate_verify(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data;
int ret = 0;
int data_size;
/* This is a packet that is only sent by the client
*/
if (state->security_parameters.entity==GNUTLS_SERVER) return 0;
/* if certificate verify is not needed just exit
*/
if (state->gnutls_internals.certificate_requested==0) return 0;
if (state->gnutls_internals.auth_struct->gnutls_generate_client_cert_vrfy==NULL) {
return 0; /* this algorithm does not support cli_cert_vrfy
*/
}
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Sending client certificate verify message\n");
#endif
data_size = state->gnutls_internals.auth_struct->gnutls_generate_client_cert_vrfy( state->gnutls_key, &data);
if (data_size < 0)
return data_size;
ret =
_gnutls_send_handshake(cd, state, data,
data_size,
GNUTLS_CERTIFICATE_VERIFY);
gnutls_free(data);
return ret;
}
int _gnutls_recv_server_kx_message(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data;
int datasize;
int ret = 0;
if (state->gnutls_internals.auth_struct->gnutls_process_server_kx!=NULL) {
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Receiving Server KX message\n");
#endif
ret =
_gnutls_recv_handshake(cd, state, &data,
&datasize,
GNUTLS_SERVER_KEY_EXCHANGE);
if (ret < 0)
return ret;
ret = state->gnutls_internals.auth_struct->gnutls_process_server_kx( state->gnutls_key, data, datasize);
gnutls_free(data);
if (ret < 0)
return ret;
}
return ret;
}
int _gnutls_recv_server_kx_message2(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data;
int datasize;
int ret = 0;
if (state->gnutls_internals.auth_struct->gnutls_process_server_kx2 != NULL) {
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Receiving Server KX message2\n");
#endif
ret =
_gnutls_recv_handshake(cd, state, &data,
&datasize,
GNUTLS_SERVER_KEY_EXCHANGE);
if (ret < 0)
return ret;
ret = state->gnutls_internals.auth_struct->gnutls_process_server_kx2( state->gnutls_key, data, datasize);
gnutls_free(data);
if (ret < 0)
return ret;
}
return ret;
}
int _gnutls_recv_client_kx_message(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data;
int datasize;
int ret = 0;
/* Do key exchange only if the algorithm permits it */
if (state->gnutls_internals.auth_struct->gnutls_process_client_kx != NULL) {
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Receiving client KX message\n");
#endif
ret =
_gnutls_recv_handshake(cd, state, &data,
&datasize,
GNUTLS_CLIENT_KEY_EXCHANGE);
if (ret < 0)
return ret;
ret = state->gnutls_internals.auth_struct->gnutls_process_client_kx( state->gnutls_key, data, datasize);
gnutls_free(data);
if (ret < 0)
return ret;
}
return ret;
}
/* only used in SRP */
int _gnutls_recv_client_kx_message0(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data;
int datasize;
int ret = 0;
/* Do key exchange only if the algorithm permits it */
if (state->gnutls_internals.auth_struct->gnutls_process_client_kx0 != NULL) {
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Receiving client KX message0\n");
#endif
ret =
_gnutls_recv_handshake(cd, state, &data,
&datasize,
GNUTLS_CLIENT_KEY_EXCHANGE);
if (ret < 0)
return ret;
ret = state->gnutls_internals.auth_struct->gnutls_process_client_kx0( state->gnutls_key, data, datasize);
gnutls_free(data);
if (ret < 0)
return ret;
}
return ret;
}
/* This is called when we want send our certificate
*/
int _gnutls_send_certificate(SOCKET cd, GNUTLS_STATE state)
{
uint8 *data = NULL;
int data_size = 0;
int ret = 0;
if (state->gnutls_internals.auth_struct->gnutls_generate_certificate==NULL)
return 0;
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Sending certificate message\n");
#endif
data_size = state->gnutls_internals.auth_struct->gnutls_generate_certificate( state->gnutls_key, &data);
if (data_size < 0) {
gnutls_assert();
return data_size;
}
ret = _gnutls_send_handshake(cd, state, data, data_size, GNUTLS_CERTIFICATE);
gnutls_free(data);
if (ret<0) {
gnutls_assert();
return ret;
}
return data_size;
}
int _gnutls_recv_certificate(SOCKET cd, GNUTLS_STATE state)
{
int datasize;
opaque * data;
int ret = 0;
if (state->gnutls_internals.auth_struct->gnutls_process_certificate!=NULL) {
ret =
_gnutls_recv_handshake(cd, state, &data,
&datasize,
GNUTLS_CERTIFICATE);
if (ret < 0) {
gnutls_assert();
return ret;
}
ret = state->gnutls_internals.auth_struct->gnutls_process_certificate( state->gnutls_key, data, datasize);
gnutls_free(data);
if (ret < 0) {
gnutls_assert();
return ret;
}
}
return ret;
}
int _gnutls_send_client_certificate(SOCKET cd, GNUTLS_STATE state)
{
if (state->gnutls_internals.certificate_requested == 0)
return 0;
#ifdef HANDSHAKE_DEBUG
_gnutls_log( "Sending Client Certificate\n");
#endif
return _gnutls_send_certificate(cd, state);
}
|