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
|
/*
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2009, 2010
* Free Software Foundation, Inc.
*
* Copyright (C) 2011
* Bardenheuer GmbH, Munich and Bundesdruckerei GmbH, Berlin
*
* Copyright (C) 2013 Frank Morgner
* Copyright (C) 2013 Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS 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
*
*/
#include <gnutls_int.h>
#ifdef ENABLE_PSK
#include "gnutls_auth.h"
#include "gnutls_dh.h"
#include "gnutls_errors.h"
#include "gnutls_mpi.h"
#include "gnutls_num.h"
#include "gnutls_int.h"
#include "gnutls_pk.h"
#include "random.h"
#include <abstract_int.h>
#include <algorithms.h>
#include <auth/dh_common.h>
#include <auth/psk.h>
#include <auth/psk_passwd.h>
#include <auth/rsa_common.h>
#include <cert.h>
#include <gnutls_datum.h>
#include <gnutls_state.h>
static int _gnutls_gen_rsa_psk_client_kx (gnutls_session_t session,
gnutls_buffer_st * data);
static int _gnutls_proc_rsa_psk_client_kx (gnutls_session_t, uint8_t *,
size_t);
const mod_auth_st rsa_psk_auth_struct = {
"RSA PSK",
_gnutls_gen_cert_server_crt,
NULL, /* generate_client_certificate */
_gnutls_gen_psk_server_kx,
_gnutls_gen_rsa_psk_client_kx,
NULL, /* generate_client_cert_vrfy */
NULL, /* generate_server_certificate_request */
_gnutls_proc_crt,
NULL, /* process_client_certificate */
_gnutls_proc_psk_server_kx,
_gnutls_proc_rsa_psk_client_kx,
NULL, /* process_client_cert_vrfy */
NULL /* process_server_certificate_reuqest */
};
/* Set the PSK premaster secret.
*/
static int
set_rsa_psk_session_key (gnutls_session_t session,
gnutls_datum_t *ppsk, gnutls_datum_t * rsa_secret)
{
unsigned char *p;
size_t rsa_secret_size;
int ret;
rsa_secret_size = rsa_secret->size;
/* set the session key
*/
session->key.key.size = 2 + rsa_secret_size + 2 + ppsk->size;
session->key.key.data = gnutls_malloc (session->key.key.size);
if (session->key.key.data == NULL)
{
gnutls_assert ();
ret = GNUTLS_E_MEMORY_ERROR;
goto error;
}
/* format of the premaster secret:
* (uint16_t) other_secret size (48)
* other_secret: 2 byte version + 46 byte random
* (uint16_t) psk_size
* the psk
*/
_gnutls_write_uint16 (rsa_secret_size, session->key.key.data);
memcpy (&session->key.key.data[2], rsa_secret->data, rsa_secret->size);
p = &session->key.key.data[rsa_secret_size + 2];
_gnutls_write_uint16 (ppsk->size, p);
if (ppsk->data != NULL)
memcpy (p + 2, ppsk->data, ppsk->size);
ret = 0;
error:
return ret;
}
/* Generate client key exchange message
*
*
* struct {
* select (KeyExchangeAlgorithm) {
* uint8_t psk_identity<0..2^16-1>;
* EncryptedPreMasterSecret;
* } exchange_keys;
* } ClientKeyExchange;
*/
static int
_gnutls_gen_rsa_psk_client_kx (gnutls_session_t session,
gnutls_buffer_st * data)
{
cert_auth_info_t auth = session->key.auth_info;
gnutls_datum_t sdata; /* data to send */
gnutls_pk_params_st params;
gnutls_psk_client_credentials_t cred;
gnutls_datum_t username, key;
int ret, free;
if (auth == NULL)
{
/* this shouldn't have happened. The proc_certificate
* function should have detected that.
*/
gnutls_assert ();
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
gnutls_datum_t premaster_secret;
premaster_secret.size = GNUTLS_MASTER_SIZE;
premaster_secret.data = gnutls_secure_malloc (premaster_secret.size);
if (premaster_secret.data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
/* Generate random */
ret = _gnutls_rnd (GNUTLS_RND_RANDOM, premaster_secret.data,
premaster_secret.size);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
/* Set version */
if (session->internals.rsa_pms_version[0] == 0)
{
premaster_secret.data[0] = _gnutls_get_adv_version_major (session);
premaster_secret.data[1] = _gnutls_get_adv_version_minor (session);
}
else
{ /* use the version provided */
premaster_secret.data[0] = session->internals.rsa_pms_version[0];
premaster_secret.data[1] = session->internals.rsa_pms_version[1];
}
/* move RSA parameters to key (session).
*/
if ((ret = _gnutls_get_public_rsa_params (session, ¶ms)) < 0)
{
gnutls_assert ();
return ret;
}
/* Encrypt premaster secret */
if ((ret =
_gnutls_pk_encrypt (GNUTLS_PK_RSA, &sdata, &premaster_secret,
¶ms)) < 0)
{
gnutls_assert ();
return ret;
}
gnutls_pk_params_release (¶ms);
cred = (gnutls_psk_client_credentials_t)
_gnutls_get_cred (session, GNUTLS_CRD_PSK, NULL);
if (cred == NULL)
{
gnutls_assert ();
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
ret = _gnutls_find_psk_key( session, cred, &username, &key, &free);
if (ret < 0)
return gnutls_assert_val(ret);
/* Here we set the PSK key */
ret = set_rsa_psk_session_key (session, &key, &premaster_secret);
if (ret < 0)
{
gnutls_assert ();
goto cleanup;
}
/* Create message for client key exchange
*
* struct {
* uint8_t psk_identity<0..2^16-1>;
* EncryptedPreMasterSecret;
* }
*/
/* Write psk_identity and EncryptedPreMasterSecret into data stream
*/
ret = _gnutls_buffer_append_data_prefix (data, 16, cred->username.data,
cred->username.size);
if (ret < 0)
{
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data_prefix (data, 16, sdata.data, sdata.size);
if (ret < 0)
{
gnutls_assert();
goto cleanup;
}
ret = 0;
cleanup:
_gnutls_free_datum (&sdata);
_gnutls_free_datum (&premaster_secret);
if (free)
{
gnutls_free(key.data);
gnutls_free(username.data);
}
return data->length;
}
/*
Process the client key exchange message
*/
static int
_gnutls_proc_rsa_psk_client_kx (gnutls_session_t session, uint8_t * data,
size_t _data_size)
{
gnutls_datum_t username;
psk_auth_info_t info;
gnutls_datum_t plaintext;
gnutls_datum_t ciphertext;
gnutls_datum_t pwd_psk = {NULL, 0};
int ret, dsize;
int randomize_key = 0;
ssize_t data_size = _data_size;
gnutls_psk_server_credentials_t cred;
gnutls_datum_t premaster_secret = {NULL, 0};
cred = (gnutls_psk_server_credentials_t)
_gnutls_get_cred (session, GNUTLS_CRD_PSK, NULL);
if (cred == NULL)
{
gnutls_assert ();
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
ret = _gnutls_auth_info_set (session, GNUTLS_CRD_PSK,
sizeof (psk_auth_info_st), 1);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
/*** 1. Extract user psk_identity ***/
DECR_LEN (data_size, 2);
username.size = _gnutls_read_uint16 (&data[0]);
DECR_LEN (data_size, username.size);
username.data = &data[2];
/* copy the username to the auth info structures
*/
info = _gnutls_get_auth_info (session);
if (username.size > MAX_USERNAME_SIZE)
{
gnutls_assert ();
return GNUTLS_E_ILLEGAL_SRP_USERNAME;
}
memcpy (info->username, username.data, username.size);
info->username[username.size] = 0;
/* Adjust data so it points to EncryptedPreMasterSecret */
data += username.size + 2;
/*** 2. Decrypt and extract EncryptedPreMasterSecret ***/
DECR_LEN (data_size, 2);
ciphertext.data = &data[2];
dsize = _gnutls_read_uint16 (data);
if (dsize != data_size)
{
gnutls_assert ();
return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
}
ciphertext.size = dsize;
ret = gnutls_privkey_decrypt_data (session->internals.selected_key, 0,
&ciphertext, &plaintext);
if (ret < 0 || plaintext.size != GNUTLS_MASTER_SIZE)
{
/* In case decryption fails then don't inform
* the peer. Just use a random key. (in order to avoid
* attack against pkcs-1 formating).
*/
gnutls_assert ();
_gnutls_debug_log ("auth_rsa_psk: Possible PKCS #1 format attack\n");
randomize_key = 1;
}
else
{
/* If the secret was properly formatted, then
* check the version number.
*/
if (_gnutls_get_adv_version_major (session) != plaintext.data[0] ||
(session->internals.priorities.allow_wrong_pms == 0 &&
_gnutls_get_adv_version_minor (session) != plaintext.data[1]))
{
/* No error is returned here, if the version number check
* fails. We proceed normally.
* That is to defend against the attack described in the paper
* "Attacking RSA-based sessions in SSL/TLS" by Vlastimil Klima,
* Ondej Pokorny and Tomas Rosa.
*/
gnutls_assert ();
_gnutls_debug_log
("auth_rsa: Possible PKCS #1 version check format attack\n");
}
}
if (randomize_key != 0)
{
premaster_secret.size = GNUTLS_MASTER_SIZE;
premaster_secret.data = gnutls_malloc (premaster_secret.size);
if (premaster_secret.data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
/* we do not need strong random numbers here.
*/
ret = _gnutls_rnd (GNUTLS_RND_NONCE, premaster_secret.data,
premaster_secret.size);
if (ret < 0)
{
gnutls_assert ();
goto cleanup;
}
}
else
{
premaster_secret.data = plaintext.data;
premaster_secret.size = plaintext.size;
}
/* This is here to avoid the version check attack
* discussed above.
*/
premaster_secret.data[0] = _gnutls_get_adv_version_major (session);
premaster_secret.data[1] = _gnutls_get_adv_version_minor (session);
/* find the key of this username
*/
ret = _gnutls_psk_pwd_find_entry (session, info->username, &pwd_psk);
if (ret < 0)
{
gnutls_assert();
goto cleanup;
}
ret = set_rsa_psk_session_key (session, &pwd_psk, &premaster_secret);
if (ret < 0)
{
gnutls_assert ();
goto cleanup;
}
ret = 0;
cleanup:
_gnutls_free_datum (&pwd_psk);
_gnutls_free_datum (&premaster_secret);
return ret;
}
#endif /* ENABLE_PSK */
|