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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/*
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008, 2010 Free Software
* Foundation, Inc.
*
* Author: 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
*
*/
/* Functions for operating in an SRP passwd file are included here */
#include <gnutls_int.h>
#ifdef ENABLE_SRP
#include "x509_b64.h"
#include "gnutls_errors.h"
#include <auth_srp_passwd.h>
#include "auth_srp.h"
#include "gnutls_auth.h"
#include "gnutls_srp.h"
#include "gnutls_dh.h"
#include "debug.h"
#include <gnutls_str.h>
#include <gnutls_datum.h>
#include <gnutls_num.h>
#include <random.h>
static int _randomize_pwd_entry (SRP_PWD_ENTRY * entry);
/* this function parses tpasswd.conf file. Format is:
* string(username):base64(v):base64(salt):int(index)
*/
static int
pwd_put_values (SRP_PWD_ENTRY * entry, char *str)
{
char *p;
int len, ret;
opaque *verifier;
size_t verifier_size;
int indx;
p = strrchr (str, ':'); /* we have index */
if (p == NULL)
{
gnutls_assert ();
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
*p = '\0';
p++;
indx = atoi (p);
if (indx == 0)
{
gnutls_assert ();
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
/* now go for salt */
p = strrchr (str, ':'); /* we have salt */
if (p == NULL)
{
gnutls_assert ();
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
*p = '\0';
p++;
len = strlen (p);
entry->salt.size = _gnutls_sbase64_decode (p, len, &entry->salt.data);
if (entry->salt.size <= 0)
{
gnutls_assert ();
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
/* now go for verifier */
p = strrchr (str, ':'); /* we have verifier */
if (p == NULL)
{
_gnutls_free_datum (&entry->salt);
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
*p = '\0';
p++;
len = strlen (p);
ret = _gnutls_sbase64_decode (p, len, &verifier);
if (ret <= 0)
{
gnutls_assert ();
_gnutls_free_datum (&entry->salt);
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
verifier_size = ret;
entry->v.data = verifier;
entry->v.size = verifier_size;
/* now go for username */
*p = '\0';
entry->username = gnutls_strdup (str);
if (entry->username == NULL)
{
_gnutls_free_datum (&entry->salt);
_gnutls_free_datum (&entry->v);
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
return indx;
}
/* this function parses tpasswd.conf file. Format is:
* int(index):base64(n):int(g)
*/
static int
pwd_put_values2 (SRP_PWD_ENTRY * entry, char *str)
{
char *p;
int len;
opaque *tmp;
int ret;
p = strrchr (str, ':'); /* we have g */
if (p == NULL)
{
gnutls_assert ();
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
*p = '\0';
p++;
/* read the generator */
len = strlen (p);
if (p[len - 1] == '\n' || p[len - 1] == ' ')
len--;
ret = _gnutls_sbase64_decode (p, len, &tmp);
if (ret < 0)
{
gnutls_assert ();
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
entry->g.data = tmp;
entry->g.size = ret;
/* now go for n - modulo */
p = strrchr (str, ':'); /* we have n */
if (p == NULL)
{
_gnutls_free_datum (&entry->g);
gnutls_assert ();
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
*p = '\0';
p++;
len = strlen (p);
ret = _gnutls_sbase64_decode (p, len, &tmp);
if (ret < 0)
{
gnutls_assert ();
_gnutls_free_datum (&entry->g);
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
}
entry->n.data = tmp;
entry->n.size = ret;
return 0;
}
/* this function opens the tpasswd.conf file and reads the g and n
* values. They are put in the entry.
*/
static int
pwd_read_conf (const char *pconf_file, SRP_PWD_ENTRY * entry, int idx)
{
FILE *fd;
char line[2 * 1024];
unsigned i, len;
char indexstr[10];
sprintf (indexstr, "%d", idx); /* Flawfinder: ignore */
fd = fopen (pconf_file, "r");
if (fd == NULL)
{
gnutls_assert ();
return GNUTLS_E_FILE_ERROR;
}
len = strlen (indexstr);
while (fgets (line, sizeof (line), fd) != NULL)
{
/* move to first ':' */
i = 0;
while ((line[i] != ':') && (line[i] != '\0') && (i < sizeof (line)))
{
i++;
}
if (strncmp (indexstr, line, MAX (i, len)) == 0)
{
if ((idx = pwd_put_values2 (entry, line)) >= 0)
return 0;
else
{
return GNUTLS_E_SRP_PWD_ERROR;
}
}
}
return GNUTLS_E_SRP_PWD_ERROR;
}
int
_gnutls_srp_pwd_read_entry (gnutls_session_t state, char *username,
SRP_PWD_ENTRY ** _entry)
{
gnutls_srp_server_credentials_t cred;
FILE *fd;
char line[2 * 1024];
unsigned i, len;
int ret;
int idx, last_idx;
SRP_PWD_ENTRY *entry;
*_entry = gnutls_calloc (1, sizeof (SRP_PWD_ENTRY));
if (*_entry == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
entry = *_entry;
cred = (gnutls_srp_server_credentials_t)
_gnutls_get_cred (state->key, GNUTLS_CRD_SRP, NULL);
if (cred == NULL)
{
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
/* if the callback which sends the parameters is
* set, use it.
*/
if (cred->pwd_callback != NULL)
{
ret = cred->pwd_callback (state, username, &entry->salt,
&entry->v, &entry->g, &entry->n);
if (ret == 1)
{ /* the user does not exist */
if (entry->g.size != 0 && entry->n.size != 0)
{
ret = _randomize_pwd_entry (entry);
if (ret < 0)
{
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return ret;
}
return 0;
}
else
{
gnutls_assert ();
ret = -1; /* error in the callback */
}
}
if (ret < 0)
{
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return GNUTLS_E_SRP_PWD_ERROR;
}
return 0;
}
/* The callback was not set. Proceed.
*/
if (cred->password_file == NULL)
{
gnutls_assert ();
return GNUTLS_E_SRP_PWD_ERROR;
}
/* Open the selected password file.
*/
fd = fopen (cred->password_file, "r");
if (fd == NULL)
{
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return GNUTLS_E_SRP_PWD_ERROR;
}
last_idx = 1; /* a default value */
len = strlen (username);
while (fgets (line, sizeof (line), fd) != NULL)
{
/* move to first ':' */
i = 0;
while ((line[i] != ':') && (line[i] != '\0') && (i < sizeof (line)))
{
i++;
}
if (strncmp (username, line, MAX (i, len)) == 0)
{
if ((idx = pwd_put_values (entry, line)) >= 0)
{
/* Keep the last index in memory, so we can retrieve fake parameters (g,n)
* when the user does not exist.
*/
/* XXX: last_idx will not be read as both if block branches return. */
last_idx = idx;
if (pwd_read_conf (cred->password_conf_file, entry, idx) == 0)
{
return 0;
}
else
{
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return GNUTLS_E_SRP_PWD_ERROR;
}
}
else
{
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return GNUTLS_E_SRP_PWD_ERROR;
}
}
}
/* user was not found. Fake him. Actually read the g,n values from
* the last index found and randomize the entry.
*/
if (pwd_read_conf (cred->password_conf_file, entry, last_idx) == 0)
{
ret = _randomize_pwd_entry (entry);
if (ret < 0)
{
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return ret;
}
return 0;
}
gnutls_assert ();
_gnutls_srp_entry_free (entry);
return GNUTLS_E_SRP_PWD_ERROR;
}
/* Randomizes the given password entry. It actually sets the verifier
* and the salt. Returns 0 on success.
*/
static int
_randomize_pwd_entry (SRP_PWD_ENTRY * entry)
{
unsigned char rnd;
int ret;
if (entry->g.size == 0 || entry->n.size == 0)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
ret = _gnutls_rnd (GNUTLS_RND_NONCE, &rnd, 1);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
entry->salt.size = (rnd % 10) + 9;
entry->v.data = gnutls_malloc (20);
entry->v.size = 20;
if (entry->v.data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
ret = _gnutls_rnd (GNUTLS_RND_RANDOM, entry->v.data, 20);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
entry->salt.data = gnutls_malloc (entry->salt.size);
if (entry->salt.data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
ret = _gnutls_rnd (GNUTLS_RND_NONCE, entry->salt.data, entry->salt.size);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
return 0;
}
/* Free all the entry parameters, except if g and n are
* the static ones defined in extra.h
*/
void
_gnutls_srp_entry_free (SRP_PWD_ENTRY * entry)
{
_gnutls_free_datum (&entry->v);
_gnutls_free_datum (&entry->salt);
if (entry->g.data != gnutls_srp_1024_group_generator.data)
_gnutls_free_datum (&entry->g);
if (entry->n.data != gnutls_srp_1024_group_prime.data &&
entry->n.data != gnutls_srp_1536_group_prime.data &&
entry->n.data != gnutls_srp_2048_group_prime.data)
_gnutls_free_datum (&entry->n);
gnutls_free (entry->username);
gnutls_free (entry);
}
#endif /* ENABLE SRP */
|