]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Christophe Renou (christophe.renou@edelweb.fr) with the | |
3 | * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the | |
4 | * EdelKey project and contributed to the OpenSSL project 2004. | |
edc032b5 BL |
5 | */ |
6 | /* ==================================================================== | |
7 | * Copyright (c) 2004 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 14 | * notice, this list of conditions and the following disclaimer. |
edc032b5 BL |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | #ifndef OPENSSL_NO_SRP | |
b39fc560 | 60 | # include "internal/cryptlib.h" |
dfb56425 | 61 | # include <openssl/sha.h> |
0f113f3e MC |
62 | # include <openssl/srp.h> |
63 | # include <openssl/evp.h> | |
64 | # include <openssl/buffer.h> | |
65 | # include <openssl/rand.h> | |
66 | # include <openssl/txt_db.h> | |
edc032b5 | 67 | |
0f113f3e MC |
68 | # define SRP_RANDOM_SALT_LEN 20 |
69 | # define MAX_LEN 2500 | |
edc032b5 BL |
70 | |
71 | static char b64table[] = | |
0f113f3e | 72 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"; |
edc032b5 | 73 | |
0f113f3e MC |
74 | /* |
75 | * the following two conversion routines have been inspired by code from | |
76 | * Stanford | |
77 | */ | |
edc032b5 BL |
78 | |
79 | /* | |
80 | * Convert a base64 string into raw byte array representation. | |
81 | */ | |
82 | static int t_fromb64(unsigned char *a, const char *src) | |
0f113f3e MC |
83 | { |
84 | char *loc; | |
85 | int i, j; | |
86 | int size; | |
87 | ||
88 | while (*src && (*src == ' ' || *src == '\t' || *src == '\n')) | |
89 | ++src; | |
90 | size = strlen(src); | |
91 | i = 0; | |
92 | while (i < size) { | |
93 | loc = strchr(b64table, src[i]); | |
94 | if (loc == (char *)0) | |
95 | break; | |
96 | else | |
97 | a[i] = loc - b64table; | |
98 | ++i; | |
99 | } | |
100 | /* if nothing valid to process we have a zero length response */ | |
101 | if (i == 0) | |
102 | return 0; | |
103 | size = i; | |
104 | i = size - 1; | |
105 | j = size; | |
106 | while (1) { | |
107 | a[j] = a[i]; | |
108 | if (--i < 0) | |
109 | break; | |
110 | a[j] |= (a[i] & 3) << 6; | |
111 | --j; | |
112 | a[j] = (unsigned char)((a[i] & 0x3c) >> 2); | |
113 | if (--i < 0) | |
114 | break; | |
115 | a[j] |= (a[i] & 0xf) << 4; | |
116 | --j; | |
117 | a[j] = (unsigned char)((a[i] & 0x30) >> 4); | |
118 | if (--i < 0) | |
119 | break; | |
120 | a[j] |= (a[i] << 2); | |
121 | ||
122 | a[--j] = 0; | |
123 | if (--i < 0) | |
124 | break; | |
125 | } | |
126 | while (a[j] == 0 && j <= size) | |
127 | ++j; | |
128 | i = 0; | |
129 | while (j <= size) | |
130 | a[i++] = a[j++]; | |
131 | return i; | |
132 | } | |
edc032b5 BL |
133 | |
134 | /* | |
135 | * Convert a raw byte string into a null-terminated base64 ASCII string. | |
136 | */ | |
137 | static char *t_tob64(char *dst, const unsigned char *src, int size) | |
0f113f3e MC |
138 | { |
139 | int c, pos = size % 3; | |
140 | unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0; | |
141 | char *olddst = dst; | |
142 | ||
143 | switch (pos) { | |
144 | case 1: | |
145 | b2 = src[0]; | |
146 | break; | |
147 | case 2: | |
148 | b1 = src[0]; | |
149 | b2 = src[1]; | |
150 | break; | |
151 | } | |
152 | ||
153 | while (1) { | |
154 | c = (b0 & 0xfc) >> 2; | |
155 | if (notleading || c != 0) { | |
156 | *dst++ = b64table[c]; | |
157 | notleading = 1; | |
158 | } | |
159 | c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4); | |
160 | if (notleading || c != 0) { | |
161 | *dst++ = b64table[c]; | |
162 | notleading = 1; | |
163 | } | |
164 | c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6); | |
165 | if (notleading || c != 0) { | |
166 | *dst++ = b64table[c]; | |
167 | notleading = 1; | |
168 | } | |
169 | c = b2 & 0x3f; | |
170 | if (notleading || c != 0) { | |
171 | *dst++ = b64table[c]; | |
172 | notleading = 1; | |
173 | } | |
174 | if (pos >= size) | |
175 | break; | |
176 | else { | |
177 | b0 = src[pos++]; | |
178 | b1 = src[pos++]; | |
179 | b2 = src[pos++]; | |
180 | } | |
181 | } | |
182 | ||
183 | *dst++ = '\0'; | |
184 | return olddst; | |
185 | } | |
edc032b5 | 186 | |
380f18ed | 187 | void SRP_user_pwd_free(SRP_user_pwd *user_pwd) |
0f113f3e MC |
188 | { |
189 | if (user_pwd == NULL) | |
190 | return; | |
191 | BN_free(user_pwd->s); | |
192 | BN_clear_free(user_pwd->v); | |
193 | OPENSSL_free(user_pwd->id); | |
194 | OPENSSL_free(user_pwd->info); | |
195 | OPENSSL_free(user_pwd); | |
196 | } | |
edc032b5 | 197 | |
71fa4513 | 198 | static SRP_user_pwd *SRP_user_pwd_new(void) |
0f113f3e | 199 | { |
b4faea50 | 200 | SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret)); |
0f113f3e MC |
201 | if (ret == NULL) |
202 | return NULL; | |
203 | ret->N = NULL; | |
204 | ret->g = NULL; | |
205 | ret->s = NULL; | |
206 | ret->v = NULL; | |
207 | ret->id = NULL; | |
208 | ret->info = NULL; | |
209 | return ret; | |
210 | } | |
edc032b5 BL |
211 | |
212 | static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g, | |
0f113f3e MC |
213 | const BIGNUM *N) |
214 | { | |
215 | vinfo->N = N; | |
216 | vinfo->g = g; | |
217 | } | |
edc032b5 BL |
218 | |
219 | static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id, | |
0f113f3e MC |
220 | const char *info) |
221 | { | |
7644a9ae | 222 | if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id))) |
0f113f3e | 223 | return 0; |
7644a9ae | 224 | return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info))); |
0f113f3e | 225 | } |
edc032b5 BL |
226 | |
227 | static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s, | |
0f113f3e MC |
228 | const char *v) |
229 | { | |
230 | unsigned char tmp[MAX_LEN]; | |
231 | int len; | |
232 | ||
233 | if (strlen(s) > MAX_LEN || strlen(v) > MAX_LEN) | |
234 | return 0; | |
235 | len = t_fromb64(tmp, v); | |
236 | if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL))) | |
237 | return 0; | |
238 | len = t_fromb64(tmp, s); | |
239 | return ((vinfo->s = BN_bin2bn(tmp, len, NULL)) != NULL); | |
240 | } | |
edc032b5 | 241 | |
71fa4513 | 242 | static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v) |
0f113f3e MC |
243 | { |
244 | vinfo->v = v; | |
245 | vinfo->s = s; | |
246 | return (vinfo->s != NULL && vinfo->v != NULL); | |
247 | } | |
edc032b5 | 248 | |
380f18ed EK |
249 | static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src) |
250 | { | |
251 | SRP_user_pwd *ret; | |
252 | ||
253 | if (src == NULL) | |
254 | return NULL; | |
255 | if ((ret = SRP_user_pwd_new()) == NULL) | |
256 | return NULL; | |
257 | ||
258 | SRP_user_pwd_set_gN(ret, src->g, src->N); | |
259 | if (!SRP_user_pwd_set_ids(ret, src->id, src->info) | |
260 | || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) { | |
261 | SRP_user_pwd_free(ret); | |
262 | return NULL; | |
263 | } | |
264 | return ret; | |
265 | } | |
266 | ||
edc032b5 | 267 | SRP_VBASE *SRP_VBASE_new(char *seed_key) |
0f113f3e | 268 | { |
b4faea50 | 269 | SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb)); |
0f113f3e MC |
270 | |
271 | if (vb == NULL) | |
272 | return NULL; | |
75ebbd9a RS |
273 | if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL |
274 | || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) { | |
0f113f3e MC |
275 | OPENSSL_free(vb); |
276 | return NULL; | |
277 | } | |
278 | vb->default_g = NULL; | |
279 | vb->default_N = NULL; | |
280 | vb->seed_key = NULL; | |
7644a9ae | 281 | if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) { |
0f113f3e MC |
282 | sk_SRP_user_pwd_free(vb->users_pwd); |
283 | sk_SRP_gN_cache_free(vb->gN_cache); | |
284 | OPENSSL_free(vb); | |
285 | return NULL; | |
286 | } | |
287 | return vb; | |
288 | } | |
edc032b5 | 289 | |
895cba19 | 290 | void SRP_VBASE_free(SRP_VBASE *vb) |
0f113f3e | 291 | { |
895cba19 RS |
292 | if (!vb) |
293 | return; | |
0f113f3e MC |
294 | sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free); |
295 | sk_SRP_gN_cache_free(vb->gN_cache); | |
296 | OPENSSL_free(vb->seed_key); | |
297 | OPENSSL_free(vb); | |
0f113f3e | 298 | } |
edc032b5 BL |
299 | |
300 | static SRP_gN_cache *SRP_gN_new_init(const char *ch) | |
0f113f3e MC |
301 | { |
302 | unsigned char tmp[MAX_LEN]; | |
303 | int len; | |
b4faea50 | 304 | SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN)); |
edc032b5 | 305 | |
0f113f3e MC |
306 | if (newgN == NULL) |
307 | return NULL; | |
edc032b5 | 308 | |
7644a9ae | 309 | if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL) |
0f113f3e | 310 | goto err; |
edc032b5 | 311 | |
0f113f3e MC |
312 | len = t_fromb64(tmp, ch); |
313 | if ((newgN->bn = BN_bin2bn(tmp, len, NULL))) | |
314 | return newgN; | |
edc032b5 | 315 | |
0f113f3e MC |
316 | OPENSSL_free(newgN->b64_bn); |
317 | err: | |
318 | OPENSSL_free(newgN); | |
319 | return NULL; | |
320 | } | |
edc032b5 BL |
321 | |
322 | static void SRP_gN_free(SRP_gN_cache *gN_cache) | |
0f113f3e MC |
323 | { |
324 | if (gN_cache == NULL) | |
325 | return; | |
326 | OPENSSL_free(gN_cache->b64_bn); | |
327 | BN_free(gN_cache->bn); | |
328 | OPENSSL_free(gN_cache); | |
329 | } | |
edc032b5 BL |
330 | |
331 | static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab) | |
0f113f3e MC |
332 | { |
333 | int i; | |
334 | ||
335 | SRP_gN *gN; | |
336 | if (gN_tab != NULL) | |
337 | for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) { | |
338 | gN = sk_SRP_gN_value(gN_tab, i); | |
339 | if (gN && (id == NULL || strcmp(gN->id, id) == 0)) | |
340 | return gN; | |
341 | } | |
342 | ||
343 | return SRP_get_default_gN(id); | |
344 | } | |
edc032b5 BL |
345 | |
346 | static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch) | |
0f113f3e MC |
347 | { |
348 | int i; | |
349 | if (gN_cache == NULL) | |
350 | return NULL; | |
351 | ||
352 | /* search if we have already one... */ | |
353 | for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) { | |
354 | SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i); | |
355 | if (strcmp(cache->b64_bn, ch) == 0) | |
356 | return cache->bn; | |
357 | } | |
358 | { /* it is the first time that we find it */ | |
359 | SRP_gN_cache *newgN = SRP_gN_new_init(ch); | |
360 | if (newgN) { | |
361 | if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0) | |
362 | return newgN->bn; | |
363 | SRP_gN_free(newgN); | |
364 | } | |
365 | } | |
366 | return NULL; | |
367 | } | |
368 | ||
369 | /* | |
370 | * this function parses verifier file. Format is: | |
edc032b5 BL |
371 | * string(index):base64(N):base64(g):0 |
372 | * string(username):base64(v):base64(salt):int(index) | |
373 | */ | |
374 | ||
edc032b5 | 375 | int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file) |
0f113f3e MC |
376 | { |
377 | int error_code; | |
378 | STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null(); | |
379 | char *last_index = NULL; | |
380 | int i; | |
381 | char **pp; | |
382 | ||
383 | SRP_gN *gN = NULL; | |
384 | SRP_user_pwd *user_pwd = NULL; | |
385 | ||
386 | TXT_DB *tmpdb = NULL; | |
387 | BIO *in = BIO_new(BIO_s_file()); | |
388 | ||
389 | error_code = SRP_ERR_OPEN_FILE; | |
390 | ||
391 | if (in == NULL || BIO_read_filename(in, verifier_file) <= 0) | |
392 | goto err; | |
393 | ||
394 | error_code = SRP_ERR_VBASE_INCOMPLETE_FILE; | |
395 | ||
396 | if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL) | |
397 | goto err; | |
398 | ||
399 | error_code = SRP_ERR_MEMORY; | |
400 | ||
401 | if (vb->seed_key) { | |
402 | last_index = SRP_get_default_gN(NULL)->id; | |
403 | } | |
404 | for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) { | |
405 | pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i); | |
406 | if (pp[DB_srptype][0] == DB_SRP_INDEX) { | |
407 | /* | |
408 | * we add this couple in the internal Stack | |
409 | */ | |
410 | ||
b4faea50 | 411 | if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL) |
0f113f3e MC |
412 | goto err; |
413 | ||
7644a9ae | 414 | if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL |
75ebbd9a RS |
415 | || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier])) |
416 | == NULL | |
417 | || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt])) | |
418 | == NULL | |
0f113f3e MC |
419 | || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0) |
420 | goto err; | |
421 | ||
422 | gN = NULL; | |
423 | ||
424 | if (vb->seed_key != NULL) { | |
425 | last_index = pp[DB_srpid]; | |
426 | } | |
427 | } else if (pp[DB_srptype][0] == DB_SRP_VALID) { | |
428 | /* it is a user .... */ | |
429 | const SRP_gN *lgN; | |
430 | ||
431 | if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) { | |
432 | error_code = SRP_ERR_MEMORY; | |
433 | if ((user_pwd = SRP_user_pwd_new()) == NULL) | |
434 | goto err; | |
435 | ||
436 | SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N); | |
437 | if (!SRP_user_pwd_set_ids | |
438 | (user_pwd, pp[DB_srpid], pp[DB_srpinfo])) | |
439 | goto err; | |
440 | ||
441 | error_code = SRP_ERR_VBASE_BN_LIB; | |
442 | if (!SRP_user_pwd_set_sv | |
443 | (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier])) | |
444 | goto err; | |
445 | ||
446 | if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0) | |
447 | goto err; | |
e771eea6 | 448 | user_pwd = NULL; /* abandon responsability */ |
0f113f3e MC |
449 | } |
450 | } | |
451 | } | |
452 | ||
453 | if (last_index != NULL) { | |
454 | /* this means that we want to simulate a default user */ | |
455 | ||
456 | if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) { | |
457 | error_code = SRP_ERR_VBASE_BN_LIB; | |
458 | goto err; | |
459 | } | |
460 | vb->default_g = gN->g; | |
461 | vb->default_N = gN->N; | |
462 | gN = NULL; | |
463 | } | |
464 | error_code = SRP_NO_ERROR; | |
edc032b5 BL |
465 | |
466 | err: | |
0f113f3e MC |
467 | /* |
468 | * there may be still some leaks to fix, if this fails, the application | |
469 | * terminates most likely | |
470 | */ | |
edc032b5 | 471 | |
0f113f3e MC |
472 | if (gN != NULL) { |
473 | OPENSSL_free(gN->id); | |
474 | OPENSSL_free(gN); | |
475 | } | |
edc032b5 | 476 | |
0f113f3e | 477 | SRP_user_pwd_free(user_pwd); |
edc032b5 | 478 | |
895cba19 | 479 | TXT_DB_free(tmpdb); |
ca3a82c3 | 480 | BIO_free_all(in); |
edc032b5 | 481 | |
0f113f3e | 482 | sk_SRP_gN_free(SRP_gN_tab); |
edc032b5 | 483 | |
0f113f3e | 484 | return error_code; |
edc032b5 | 485 | |
0f113f3e | 486 | } |
edc032b5 | 487 | |
380f18ed | 488 | static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username) |
0f113f3e MC |
489 | { |
490 | int i; | |
491 | SRP_user_pwd *user; | |
0f113f3e MC |
492 | |
493 | if (vb == NULL) | |
494 | return NULL; | |
380f18ed | 495 | |
0f113f3e MC |
496 | for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) { |
497 | user = sk_SRP_user_pwd_value(vb->users_pwd, i); | |
498 | if (strcmp(user->id, username) == 0) | |
499 | return user; | |
500 | } | |
380f18ed EK |
501 | |
502 | return NULL; | |
503 | } | |
504 | ||
1e45206f | 505 | #if OPENSSL_API_COMPAT < 0x10100000L |
380f18ed EK |
506 | /* |
507 | * DEPRECATED: use SRP_VBASE_get1_by_user instead. | |
508 | * This method ignores the configured seed and fails for an unknown user. | |
509 | * Ownership of the returned pointer is not released to the caller. | |
510 | * In other words, caller must not free the result. | |
511 | */ | |
512 | SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username) | |
513 | { | |
514 | return find_user(vb, username); | |
515 | } | |
1e45206f | 516 | #endif |
380f18ed EK |
517 | |
518 | /* | |
519 | * Ownership of the returned pointer is released to the caller. | |
520 | * In other words, caller must free the result once done. | |
521 | */ | |
522 | SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username) | |
523 | { | |
524 | SRP_user_pwd *user; | |
525 | unsigned char digv[SHA_DIGEST_LENGTH]; | |
526 | unsigned char digs[SHA_DIGEST_LENGTH]; | |
527 | EVP_MD_CTX *ctxt = NULL; | |
528 | ||
529 | if (vb == NULL) | |
530 | return NULL; | |
531 | ||
532 | if ((user = find_user(vb, username)) != NULL) | |
533 | return srp_user_pwd_dup(user); | |
534 | ||
0f113f3e MC |
535 | if ((vb->seed_key == NULL) || |
536 | (vb->default_g == NULL) || (vb->default_N == NULL)) | |
537 | return NULL; | |
edc032b5 BL |
538 | |
539 | /* if the user is unknown we set parameters as well if we have a seed_key */ | |
540 | ||
0f113f3e MC |
541 | if ((user = SRP_user_pwd_new()) == NULL) |
542 | return NULL; | |
543 | ||
544 | SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N); | |
edc032b5 | 545 | |
0f113f3e MC |
546 | if (!SRP_user_pwd_set_ids(user, username, NULL)) |
547 | goto err; | |
548 | ||
266483d2 MC |
549 | if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0) |
550 | goto err; | |
bfb0641f | 551 | ctxt = EVP_MD_CTX_new(); |
6e59a892 RL |
552 | EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL); |
553 | EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key)); | |
554 | EVP_DigestUpdate(ctxt, username, strlen(username)); | |
555 | EVP_DigestFinal_ex(ctxt, digs, NULL); | |
bfb0641f | 556 | EVP_MD_CTX_free(ctxt); |
6e59a892 RL |
557 | ctxt = NULL; |
558 | if (SRP_user_pwd_set_sv_BN(user, | |
559 | BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL), | |
560 | BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL))) | |
0f113f3e MC |
561 | return user; |
562 | ||
895cba19 | 563 | err: |
bfb0641f | 564 | EVP_MD_CTX_free(ctxt); |
895cba19 | 565 | SRP_user_pwd_free(user); |
0f113f3e MC |
566 | return NULL; |
567 | } | |
edc032b5 BL |
568 | |
569 | /* | |
0f113f3e MC |
570 | * create a verifier (*salt,*verifier,g and N are in base64) |
571 | */ | |
edc032b5 | 572 | char *SRP_create_verifier(const char *user, const char *pass, char **salt, |
0f113f3e MC |
573 | char **verifier, const char *N, const char *g) |
574 | { | |
575 | int len; | |
bf95cde2 | 576 | char *result = NULL, *vf = NULL; |
0f113f3e MC |
577 | BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL; |
578 | unsigned char tmp[MAX_LEN]; | |
579 | unsigned char tmp2[MAX_LEN]; | |
580 | char *defgNid = NULL; | |
bf95cde2 | 581 | int vfsize = 0; |
0f113f3e MC |
582 | |
583 | if ((user == NULL) || | |
584 | (pass == NULL) || (salt == NULL) || (verifier == NULL)) | |
585 | goto err; | |
586 | ||
587 | if (N) { | |
75ebbd9a | 588 | if ((len = t_fromb64(tmp, N)) == 0) |
0f113f3e MC |
589 | goto err; |
590 | N_bn = BN_bin2bn(tmp, len, NULL); | |
75ebbd9a | 591 | if ((len = t_fromb64(tmp, g)) == 0) |
0f113f3e MC |
592 | goto err; |
593 | g_bn = BN_bin2bn(tmp, len, NULL); | |
594 | defgNid = "*"; | |
595 | } else { | |
596 | SRP_gN *gN = SRP_get_gN_by_id(g, NULL); | |
597 | if (gN == NULL) | |
598 | goto err; | |
599 | N_bn = gN->N; | |
600 | g_bn = gN->g; | |
601 | defgNid = gN->id; | |
602 | } | |
603 | ||
604 | if (*salt == NULL) { | |
266483d2 MC |
605 | if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0) |
606 | goto err; | |
0f113f3e MC |
607 | |
608 | s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL); | |
609 | } else { | |
75ebbd9a | 610 | if ((len = t_fromb64(tmp2, *salt)) == 0) |
0f113f3e MC |
611 | goto err; |
612 | s = BN_bin2bn(tmp2, len, NULL); | |
613 | } | |
614 | ||
615 | if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn)) | |
616 | goto err; | |
617 | ||
618 | BN_bn2bin(v, tmp); | |
bf95cde2 MC |
619 | vfsize = BN_num_bytes(v) * 2; |
620 | if (((vf = OPENSSL_malloc(vfsize)) == NULL)) | |
0f113f3e MC |
621 | goto err; |
622 | t_tob64(vf, tmp, BN_num_bytes(v)); | |
623 | ||
0f113f3e MC |
624 | if (*salt == NULL) { |
625 | char *tmp_salt; | |
626 | ||
627 | if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) { | |
0f113f3e MC |
628 | goto err; |
629 | } | |
630 | t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN); | |
631 | *salt = tmp_salt; | |
632 | } | |
633 | ||
bf95cde2 MC |
634 | *verifier = vf; |
635 | vf = NULL; | |
0f113f3e MC |
636 | result = defgNid; |
637 | ||
638 | err: | |
639 | if (N) { | |
640 | BN_free(N_bn); | |
641 | BN_free(g_bn); | |
642 | } | |
bf95cde2 MC |
643 | OPENSSL_clear_free(vf, vfsize); |
644 | BN_clear_free(s); | |
645 | BN_clear_free(v); | |
0f113f3e MC |
646 | return result; |
647 | } | |
edc032b5 BL |
648 | |
649 | /* | |
bf95cde2 MC |
650 | * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL |
651 | * then the provided salt will be used. On successful exit *verifier will point | |
652 | * to a newly allocated BIGNUM containing the verifier and (if a salt was not | |
653 | * provided) *salt will be populated with a newly allocated BIGNUM containing a | |
654 | * random salt. | |
655 | * The caller is responsible for freeing the allocated *salt and *verifier | |
656 | * BIGNUMS. | |
0f113f3e | 657 | */ |
8892ce77 | 658 | int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt, |
0f113f3e MC |
659 | BIGNUM **verifier, const BIGNUM *N, |
660 | const BIGNUM *g) | |
661 | { | |
662 | int result = 0; | |
663 | BIGNUM *x = NULL; | |
664 | BN_CTX *bn_ctx = BN_CTX_new(); | |
665 | unsigned char tmp2[MAX_LEN]; | |
bf95cde2 | 666 | BIGNUM *salttmp = NULL; |
edc032b5 | 667 | |
0f113f3e MC |
668 | if ((user == NULL) || |
669 | (pass == NULL) || | |
670 | (salt == NULL) || | |
671 | (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL)) | |
672 | goto err; | |
edc032b5 | 673 | |
0f113f3e | 674 | if (*salt == NULL) { |
266483d2 MC |
675 | if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0) |
676 | goto err; | |
edc032b5 | 677 | |
bf95cde2 MC |
678 | salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL); |
679 | } else { | |
680 | salttmp = *salt; | |
0f113f3e | 681 | } |
edc032b5 | 682 | |
bf95cde2 | 683 | x = SRP_Calc_x(salttmp, user, pass); |
edc032b5 | 684 | |
0f113f3e MC |
685 | *verifier = BN_new(); |
686 | if (*verifier == NULL) | |
687 | goto err; | |
edc032b5 | 688 | |
0f113f3e MC |
689 | if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) { |
690 | BN_clear_free(*verifier); | |
691 | goto err; | |
692 | } | |
edc032b5 | 693 | |
0f113f3e | 694 | result = 1; |
bf95cde2 | 695 | *salt = salttmp; |
edc032b5 | 696 | |
0f113f3e | 697 | err: |
3bbd1d63 | 698 | if (salt != NULL && *salt != salttmp) |
bf95cde2 | 699 | BN_clear_free(salttmp); |
0f113f3e MC |
700 | BN_clear_free(x); |
701 | BN_CTX_free(bn_ctx); | |
702 | return result; | |
703 | } | |
edc032b5 BL |
704 | |
705 | #endif |