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
|
/*
* Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GNUTLS.
*
* The GNUTLS library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA
*
*/
/* This file contains common stuff in Ephemeral Diffie Hellman (DHE) and
* Anonymous DH key exchange(DHA). These are used in the handshake procedure
* of the certificate and anoymous authentication.
*/
#include "gnutls_int.h"
#include "gnutls_auth_int.h"
#include "gnutls_errors.h"
#include "gnutls_dh.h"
#include "gnutls_num.h"
#include "gnutls_sig.h"
#include <gnutls_datum.h>
#include <gnutls_x509.h>
#include <gnutls_state.h>
#include <auth_dh_common.h>
#include <gnutls_algorithms.h>
/* Frees the MHD_gtls_dh_info_st structure.
*/
void
MHD_gtls_free_dh_info (MHD_gtls_dh_info_st * dh)
{
dh->secret_bits = 0;
MHD__gnutls_free_datum (&dh->prime);
MHD__gnutls_free_datum (&dh->generator);
MHD__gnutls_free_datum (&dh->public_key);
}
int
MHD_gtls_proc_dh_common_client_kx (MHD_gtls_session_t session,
opaque * data, size_t _data_size,
mpi_t g, mpi_t p)
{
uint16_t n_Y;
size_t _n_Y;
int ret;
ssize_t data_size = _data_size;
DECR_LEN (data_size, 2);
n_Y = MHD_gtls_read_uint16 (&data[0]);
_n_Y = n_Y;
DECR_LEN (data_size, n_Y);
if (MHD_gtls_mpi_scan_nz (&session->key->client_Y, &data[2], &_n_Y))
{
MHD_gnutls_assert ();
return GNUTLS_E_MPI_SCAN_FAILED;
}
MHD_gtls_dh_set_peer_public (session, session->key->client_Y);
session->key->KEY =
MHD_gtls_calc_dh_key (session->key->client_Y, session->key->dh_secret, p);
if (session->key->KEY == NULL)
{
MHD_gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
MHD_gtls_mpi_release (&session->key->client_Y);
MHD_gtls_mpi_release (&session->key->dh_secret);
ret = MHD_gtls_mpi_dprint (&session->key->key, session->key->KEY);
MHD_gtls_mpi_release (&session->key->KEY);
if (ret < 0)
{
return ret;
}
return 0;
}
int
MHD_gtls_gen_dh_common_client_kx (MHD_gtls_session_t session, opaque ** data)
{
mpi_t x = NULL, X = NULL;
size_t n_X;
int ret;
*data = NULL;
X = MHD_gtls_calc_dh_secret (&x, session->key->client_g,
session->key->client_p);
if (X == NULL || x == NULL)
{
MHD_gnutls_assert ();
ret = GNUTLS_E_MEMORY_ERROR;
goto error;
}
MHD_gtls_dh_set_secret_bits (session, MHD__gnutls_mpi_get_nbits (x));
MHD_gtls_mpi_print (NULL, &n_X, X);
(*data) = MHD_gnutls_malloc (n_X + 2);
if (*data == NULL)
{
ret = GNUTLS_E_MEMORY_ERROR;
goto error;
}
MHD_gtls_mpi_print (&(*data)[2], &n_X, X);
MHD_gtls_mpi_release (&X);
MHD_gtls_write_uint16 (n_X, &(*data)[0]);
/* calculate the key after calculating the message */
session->key->KEY =
MHD_gtls_calc_dh_key (session->key->client_Y, x, session->key->client_p);
MHD_gtls_mpi_release (&x);
if (session->key->KEY == NULL)
{
MHD_gnutls_assert ();
ret = GNUTLS_E_MEMORY_ERROR;
goto error;
}
/* THESE SHOULD BE DISCARDED */
MHD_gtls_mpi_release (&session->key->client_Y);
MHD_gtls_mpi_release (&session->key->client_p);
MHD_gtls_mpi_release (&session->key->client_g);
ret = MHD_gtls_mpi_dprint (&session->key->key, session->key->KEY);
MHD_gtls_mpi_release (&session->key->KEY);
if (ret < 0)
{
MHD_gnutls_assert ();
goto error;
}
return n_X + 2;
error:
MHD_gtls_mpi_release (&x);
MHD_gtls_mpi_release (&X);
MHD_gnutls_free (*data);
*data = NULL;
return ret;
}
int
MHD_gtls_proc_dh_common_server_kx (MHD_gtls_session_t session,
opaque * data, size_t _data_size, int psk)
{
uint16_t n_Y, n_g, n_p;
size_t _n_Y, _n_g, _n_p;
uint8_t *data_p;
uint8_t *data_g;
uint8_t *data_Y;
int i, bits, psk_size, ret;
ssize_t data_size = _data_size;
i = 0;
if (psk != 0)
{
DECR_LEN (data_size, 2);
psk_size = MHD_gtls_read_uint16 (&data[i]);
DECR_LEN (data_size, psk_size);
i += 2 + psk_size;
}
DECR_LEN (data_size, 2);
n_p = MHD_gtls_read_uint16 (&data[i]);
i += 2;
DECR_LEN (data_size, n_p);
data_p = &data[i];
i += n_p;
DECR_LEN (data_size, 2);
n_g = MHD_gtls_read_uint16 (&data[i]);
i += 2;
DECR_LEN (data_size, n_g);
data_g = &data[i];
i += n_g;
DECR_LEN (data_size, 2);
n_Y = MHD_gtls_read_uint16 (&data[i]);
i += 2;
DECR_LEN (data_size, n_Y);
data_Y = &data[i];
i += n_Y;
_n_Y = n_Y;
_n_g = n_g;
_n_p = n_p;
if (MHD_gtls_mpi_scan_nz (&session->key->client_Y, data_Y, &_n_Y) != 0)
{
MHD_gnutls_assert ();
return GNUTLS_E_MPI_SCAN_FAILED;
}
if (MHD_gtls_mpi_scan_nz (&session->key->client_g, data_g, &_n_g) != 0)
{
MHD_gnutls_assert ();
return GNUTLS_E_MPI_SCAN_FAILED;
}
if (MHD_gtls_mpi_scan_nz (&session->key->client_p, data_p, &_n_p) != 0)
{
MHD_gnutls_assert ();
return GNUTLS_E_MPI_SCAN_FAILED;
}
bits = MHD_gtls_dh_get_allowed_prime_bits (session);
if (bits < 0)
{
MHD_gnutls_assert ();
return bits;
}
if (MHD__gnutls_mpi_get_nbits (session->key->client_p) < (size_t) bits)
{
/* the prime used by the peer is not acceptable
*/
MHD_gnutls_assert ();
return GNUTLS_E_DH_PRIME_UNACCEPTABLE;
}
MHD_gtls_dh_set_group (session, session->key->client_g,
session->key->client_p);
MHD_gtls_dh_set_peer_public (session, session->key->client_Y);
ret = n_Y + n_p + n_g + 6;
if (psk != 0)
ret += 2;
return ret;
}
/* If the psk flag is set, then an empty psk_identity_hint will
* be inserted */
int
MHD_gtls_dh_common_print_server_kx (MHD_gtls_session_t session,
mpi_t g, mpi_t p, opaque ** data, int psk)
{
mpi_t x, X;
size_t n_X, n_g, n_p;
int ret, data_size, pos;
uint8_t *pdata;
X = MHD_gtls_calc_dh_secret (&x, g, p);
if (X == NULL || x == NULL)
{
MHD_gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
session->key->dh_secret = x;
MHD_gtls_dh_set_secret_bits (session, MHD__gnutls_mpi_get_nbits (x));
MHD_gtls_mpi_print (NULL, &n_g, g);
MHD_gtls_mpi_print (NULL, &n_p, p);
MHD_gtls_mpi_print (NULL, &n_X, X);
data_size = n_g + n_p + n_X + 6;
if (psk != 0)
data_size += 2;
(*data) = MHD_gnutls_malloc (data_size);
if (*data == NULL)
{
MHD_gtls_mpi_release (&X);
return GNUTLS_E_MEMORY_ERROR;
}
pos = 0;
pdata = *data;
if (psk != 0)
{
MHD_gtls_write_uint16 (0, &pdata[pos]);
pos += 2;
}
MHD_gtls_mpi_print (&pdata[pos + 2], &n_p, p);
MHD_gtls_write_uint16 (n_p, &pdata[pos]);
pos += n_p + 2;
MHD_gtls_mpi_print (&pdata[pos + 2], &n_g, g);
MHD_gtls_write_uint16 (n_g, &pdata[pos]);
pos += n_g + 2;
MHD_gtls_mpi_print (&pdata[pos + 2], &n_X, X);
MHD_gtls_mpi_release (&X);
MHD_gtls_write_uint16 (n_X, &pdata[pos]);
ret = data_size;
return ret;
}
|