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
|
/*
* Copyright (C) 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
*/
/* Functions for operating in an SRP passwd file are included here */
#include "gnutls_int.h"
#include "x509_b64.h"
#include "gnutls_errors.h"
#include "auth_srp_passwd.h"
#include "auth_srp.h"
#include "gnutls_auth_int.h"
#include "gnutls_srp.h"
#include "gnutls_random.h"
#include "debug.h"
/* this function parses tpasswd.conf file. Format is:
* string(username):base64(v):base64(salt):int(index)
*/
static int pwd_put_values( GNUTLS_SRP_PWD_ENTRY *entry, char *str, int str_size) {
char * p, *p2;
int len;
opaque *verifier;
int verifier_size;
int indx;
p = rindex( str, ':'); /* we have index */
if (p==NULL) {
gnutls_assert();
return GNUTLS_E_PARSING_ERROR;
}
*p='\0';
p++;
len = strlen(p);
indx = atoi(p);
if (indx==0) {
gnutls_assert();
return GNUTLS_E_PARSING_ERROR;
}
/* now go for salt */
p = rindex( str, ':'); /* we have salt */
if (p==NULL) {
gnutls_assert();
return GNUTLS_E_PARSING_ERROR;
}
*p='\0';
p++;
len = strlen(p);
entry->salt_size = _gnutls_sbase64_decode( p, len, &entry->salt);
if (entry->salt_size <= 0) {
gnutls_assert();
return GNUTLS_E_PARSING_ERROR;
}
/* now go for verifier */
p = rindex( str, ':'); /* we have verifier */
if (p==NULL) {
gnutls_free(entry->salt);
return GNUTLS_E_PARSING_ERROR;
}
*p='\0';
p++;
if ( (p2 = index(p, '$')) == NULL) {
entry->algorithm = SRPSHA1_CRYPT;
} else {
p++;
entry->algorithm = atoi(p);
p2 = index(p, '$'); /* find the last $ */
if (p2==NULL) {
gnutls_assert();
gnutls_free(entry->salt);
return GNUTLS_E_PARSING_ERROR;
}
p = p2+1;
}
len = strlen(p);
verifier_size = _gnutls_sbase64_decode( p, len, &verifier);
if (verifier_size <= 0) {
gnutls_assert();
gnutls_free(entry->salt);
return GNUTLS_E_PARSING_ERROR;
}
if (gcry_mpi_scan(&entry->v, GCRYMPI_FMT_USG, verifier, &verifier_size) || entry->v == NULL) {
gnutls_assert();
gnutls_free(entry->salt);
return GNUTLS_E_MPI_SCAN_FAILED;
}
/* now go for username */
*p='\0';
entry->username = gnutls_strdup(str);
return indx;
}
/* this function parses tpasswd.conf file. Format is:
* int(index):base64(n):int(g)
*/
static int pwd_put_values2( GNUTLS_SRP_PWD_ENTRY *entry, char *str, int str_size) {
char * p;
int len;
opaque * tmp;
int tmp_size;
p = rindex( str, ':'); /* we have g */
if (p==NULL) {
gnutls_assert();
return GNUTLS_E_PARSING_ERROR;
}
*p='\0';
p++;
/* read the generator */
len = strlen(p);
if (p[len-1]=='\n' || p[len-1]==' ') len--;
tmp_size = _gnutls_sbase64_decode( p, len, &tmp);
if (tmp_size < 0) {
gnutls_assert();
return GNUTLS_E_PARSING_ERROR;
}
if (gcry_mpi_scan(&entry->g, GCRYMPI_FMT_USG, tmp, &tmp_size) || entry->g==NULL) {
gnutls_assert();
gnutls_free(tmp);
return GNUTLS_E_MPI_SCAN_FAILED;
}
gnutls_free(tmp);
/* now go for n - modulo */
p = rindex( str, ':'); /* we have n */
if (p==NULL) {
_gnutls_mpi_release(&entry->g);
gnutls_assert();
return GNUTLS_E_PARSING_ERROR;
}
*p='\0';
p++;
len = strlen(p);
tmp_size = _gnutls_sbase64_decode( p, len, &tmp);
if (tmp_size < 0) {
gnutls_assert();
_gnutls_mpi_release(&entry->g);
return GNUTLS_E_PARSING_ERROR;
}
if (gcry_mpi_scan(&entry->n, GCRYMPI_FMT_USG, tmp, &tmp_size) || entry->n==NULL) {
gnutls_assert();
gnutls_free(tmp);
_gnutls_mpi_release(&entry->g);
return GNUTLS_E_MPI_SCAN_FAILED;
}
gnutls_free(tmp);
return 0;
}
/* this function opens the tpasswd.conf file
*/
static int pwd_read_conf( const SRP_SERVER_CREDENTIALS cred, GNUTLS_SRP_PWD_ENTRY* entry, int index) {
FILE * fd;
char line[5*1024];
int i;
char indexstr[10];
sprintf( indexstr, "%d", index);
fd = fopen( cred->password_conf_file, "r");
if (fd==NULL) {
gnutls_assert();
gnutls_free(entry);
return GNUTLS_E_PWD_ERROR;
}
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, strlen(indexstr)) == 0) {
if ((index = pwd_put_values2( entry, line, strlen(line))) >= 0)
return 0;
else {
return GNUTLS_E_PWD_ERROR;
}
}
}
return GNUTLS_E_PWD_ERROR;
}
GNUTLS_SRP_PWD_ENTRY *_gnutls_srp_pwd_read_entry( GNUTLS_KEY key, char* username, int *err) {
const SRP_SERVER_CREDENTIALS cred;
FILE * fd;
char line[5*1024];
int i, len;
GNUTLS_SRP_PWD_ENTRY * entry = gnutls_malloc(sizeof(GNUTLS_SRP_PWD_ENTRY));
int index;
if (entry==NULL) {
gnutls_assert();
*err = 1;
return NULL;
}
*err = 0; /* normal exit */
cred = _gnutls_get_cred( key, GNUTLS_SRP, NULL);
if (cred==NULL) {
*err = 1;
gnutls_assert();
gnutls_free(entry);
return NULL;
}
fd = fopen( cred->password_file, "r");
if (fd==NULL) {
*err = 1; /* failed due to critical error */
gnutls_assert();
gnutls_free(entry);
return NULL;
}
while( fgets( line, sizeof(line), fd) != NULL) {
/* move to first ':' */
i=0;
while( (line[i]!=':') && (line[i]!='\0') && (i < sizeof(line)) ) {
i++;
}
len = strlen(username);
if (strncmp( username, line, (i>len)?i:len) == 0) {
if ((index = pwd_put_values( entry, line, strlen(line))) >= 0)
if (pwd_read_conf( cred, entry, index)==0) {
return entry;
} else {
gnutls_free(entry);
return NULL;
}
else {
gnutls_free(entry);
return NULL;
}
}
}
return NULL;
}
#define RNDUSER "rnd"
#define RND_SALT_SIZE 17
GNUTLS_SRP_PWD_ENTRY* _gnutls_randomize_pwd_entry() {
GNUTLS_SRP_PWD_ENTRY * pwd_entry = gnutls_malloc(sizeof(GNUTLS_SRP_PWD_ENTRY));
size_t n = sizeof diffie_hellman_group1_prime;
if (pwd_entry == NULL) {
gnutls_assert();
return NULL;
}
pwd_entry->username = gnutls_malloc(strlen(RNDUSER)+1);
if (pwd_entry->username == NULL) {
gnutls_assert();
return NULL;
}
strcpy( pwd_entry->username, RNDUSER);
pwd_entry->g = gcry_mpi_set_ui(NULL, SRP_G);
if (pwd_entry->g==NULL) {
gnutls_assert();
return NULL;
}
pwd_entry->v = gcry_mpi_new(160);
if (pwd_entry->v==NULL) {
gnutls_assert();
return NULL;
}
gcry_mpi_randomize( pwd_entry->v, 160, GCRY_WEAK_RANDOM);
if (gcry_mpi_scan(&pwd_entry->n, GCRYMPI_FMT_USG,
diffie_hellman_group1_prime, &n) !=0 || pwd_entry->n==NULL) {
gnutls_assert();
return NULL;
}
pwd_entry->salt_size = RND_SALT_SIZE;
pwd_entry->salt = gnutls_malloc(RND_SALT_SIZE);
if (pwd_entry->salt==NULL) {
gnutls_assert();
return NULL;
}
if (_gnutls_get_random(pwd_entry->salt, RND_SALT_SIZE, GNUTLS_WEAK_RANDOM) < 0) {
gnutls_assert();
return NULL;
}
pwd_entry->algorithm = 0;
return pwd_entry;
}
void _gnutls_srp_clear_pwd_entry( GNUTLS_SRP_PWD_ENTRY * entry) {
_gnutls_mpi_release(&entry->v);
_gnutls_mpi_release(&entry->g);
_gnutls_mpi_release(&entry->n);
gnutls_free(entry->salt);
gnutls_free(entry->username);
gnutls_free(entry);
return;
}
|