summaryrefslogtreecommitdiff
path: root/libextra/auth_srp_passwd.c
blob: a39bbd44ba5b5c8b1efcb7a7222c783d706beb72 (plain)
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
/*
 *      Copyright (C) 2001 Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 * GNUTLS-EXTRA 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-EXTRA 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"

#ifdef ENABLE_SRP

#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 "gnutls_dh.h"
#include "debug.h"
#include <gnutls_str.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;
int len, ret;
opaque *verifier;
size_t 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++;

	len = strlen(p);
	ret = _gnutls_sbase64_decode( p, len, &verifier);
	if (ret <= 0) {
		gnutls_assert();
		gnutls_free(entry->salt);
		return GNUTLS_E_PARSING_ERROR;
	}

	verifier_size = ret;
	if (_gnutls_mpi_scan(&entry->v, verifier, &verifier_size)) {
		gnutls_assert();
		gnutls_free( entry->salt);
		return GNUTLS_E_MPI_SCAN_FAILED;
	}

	gnutls_free( verifier);

	/* now go for username */
	*p='\0';

	entry->username = gnutls_strdup(str);
	if (entry->username==NULL) {
		gnutls_free( entry->salt);
		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( GNUTLS_SRP_PWD_ENTRY *entry, char *str, int str_size) {
char * p;
int len;
opaque * tmp;
int ret;
size_t 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--;
	ret = _gnutls_sbase64_decode( p, len, &tmp);

	if (ret < 0) {
		gnutls_assert();
		return GNUTLS_E_PARSING_ERROR;
	}

	tmp_size = ret;
	if (_gnutls_mpi_scan(&entry->g, tmp, &tmp_size)) {
		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);
	ret = _gnutls_sbase64_decode( p, len, &tmp);

	if (ret < 0) {
		gnutls_assert();
		_gnutls_mpi_release(&entry->g);
		return GNUTLS_E_PARSING_ERROR;
	}
	tmp_size = ret;
	if (_gnutls_mpi_scan(&entry->n, tmp, &tmp_size)) {
		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 char* pconf_file, GNUTLS_SRP_PWD_ENTRY* entry, int index) {
	FILE * fd;
	char line[2*1024];
	int i;
	char indexstr[10];

	sprintf( indexstr, "%d", index); /* Flawfinder: ignore */

	fd = fopen( pconf_file, "r");
	if (fd==NULL) {
		gnutls_assert();
		return GNUTLS_E_FILE_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_session state, char* username, int *err) {
	const gnutls_srp_server_credentials cred;
	FILE * fd;
	char line[2*1024];
	int i, len;
	GNUTLS_SRP_PWD_ENTRY * entry = gnutls_malloc(sizeof(GNUTLS_SRP_PWD_ENTRY));
	int index;
	int pwd_index = 0;
	
	if (entry==NULL) {
		gnutls_assert();
		*err = 1;
		return NULL;
	}

	*err = 0; /* normal exit */
	
	cred = _gnutls_get_cred( state->gnutls_key, GNUTLS_CRD_SRP, NULL);
	if (cred==NULL) {
		*err = 1;
		gnutls_assert();
		gnutls_free(entry);
		return NULL;
	}

	if (cred->password_files<=0) {
		gnutls_assert();
		return NULL;
	}
	
	/* use the callback to select a password file */
	if (state->internals.server_srp_callback!=NULL) {
		pwd_index = state->internals.server_srp_callback(
			state, cred->password_file, cred->password_conf_file,
				cred->password_files);

		if (pwd_index < 0) {
			gnutls_assert();
			return NULL;
		}
	}

	fd = fopen( cred->password_file[pwd_index], "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->password_conf_file[pwd_index], 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_calloc(1, sizeof(GNUTLS_SRP_PWD_ENTRY));
	
	if (pwd_entry == NULL) {
		gnutls_assert(); 
		return NULL;
	}
	
	pwd_entry->g = _gnutls_get_rnd_srp_params( &pwd_entry->n, 1024);
	if (pwd_entry->g==NULL || pwd_entry->n==NULL) {
		gnutls_assert();
		_gnutls_srp_clear_pwd_entry( pwd_entry);
		return NULL;
	}
	
	pwd_entry->username = gnutls_malloc(strlen(RNDUSER)+1);
	if (pwd_entry->username == NULL) {
		gnutls_assert();
		_gnutls_srp_clear_pwd_entry( pwd_entry);
		return NULL;
	}
	_gnutls_str_cpy( pwd_entry->username, MAX_SRP_USERNAME, RNDUSER); /* Flawfinder: ignore */
	
	pwd_entry->v = _gnutls_mpi_new(160);
	if (pwd_entry->v==NULL) {
		gnutls_assert();
		_gnutls_srp_clear_pwd_entry( pwd_entry);
		return NULL;
	}

        _gnutls_mpi_randomize( pwd_entry->v, 160, GCRY_WEAK_RANDOM);

	pwd_entry->salt_size = RND_SALT_SIZE;
	
	pwd_entry->salt = gnutls_malloc(RND_SALT_SIZE);
	if (pwd_entry->salt==NULL) {
		gnutls_assert();
		_gnutls_srp_clear_pwd_entry( pwd_entry);
		return NULL;
	}
	
	if (_gnutls_get_random(pwd_entry->salt, RND_SALT_SIZE, GNUTLS_WEAK_RANDOM) < 0) {
		gnutls_assert();
		_gnutls_srp_clear_pwd_entry( pwd_entry);
		return NULL;
	}
	
	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;
}

/* Generates a prime and a generator, and returns the srpbase64 encoded value.
 */
int _gnutls_srp_generate_prime(opaque ** ret_g, opaque ** ret_n, int bits)
{

	GNUTLS_MPI prime, g;
	size_t siz;
	char *tmp;

	if ( _gnutls_dh_generate_prime(&g, &prime, bits) < 0) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

	siz = 0;
	_gnutls_mpi_print( NULL, &siz, g);
	if (ret_g != NULL) {
		tmp = gnutls_malloc(siz);
		if (tmp==NULL) return GNUTLS_E_MEMORY_ERROR;
		
		_gnutls_mpi_print( tmp, &siz, g);

		if (_gnutls_sbase64_encode(tmp, siz, ret_g) < 0) {
			gnutls_free(tmp);
			return GNUTLS_E_UNKNOWN_ERROR;
		}
		gnutls_free(tmp);
	}

	siz = 0;
	_gnutls_mpi_print( NULL, &siz, prime);
	if (ret_n != NULL) {
		tmp = gnutls_malloc(siz);
		if (tmp==NULL) return GNUTLS_E_MEMORY_ERROR;

		_gnutls_mpi_print( tmp, &siz, prime);
		if (_gnutls_sbase64_encode(tmp, siz, ret_n) < 0) {
			gnutls_free(tmp);
			return GNUTLS_E_UNKNOWN_ERROR;
		}

		gnutls_free(tmp);
	}

	_gnutls_mpi_release(&g);
	_gnutls_mpi_release(&prime);

	return 0;

}

#endif /* ENABLE SRP */