]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/srp/srp_vfy.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / crypto / srp / srp_vfy.c
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.
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
14 * notice, this list of conditions and the following disclaimer.
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
60 # include "internal/cryptlib.h"
61 # include <openssl/sha.h>
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>
67
68 # define SRP_RANDOM_SALT_LEN 20
69 # define MAX_LEN 2500
70
71 static char b64table[] =
72 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
73
74 /*
75 * the following two conversion routines have been inspired by code from
76 * Stanford
77 */
78
79 /*
80 * Convert a base64 string into raw byte array representation.
81 */
82 static int t_fromb64(unsigned char *a, const char *src)
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 }
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)
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 }
186
187 static void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
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 }
197
198 static SRP_user_pwd *SRP_user_pwd_new(void)
199 {
200 SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret));
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 }
211
212 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
213 const BIGNUM *N)
214 {
215 vinfo->N = N;
216 vinfo->g = g;
217 }
218
219 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
220 const char *info)
221 {
222 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
223 return 0;
224 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
225 }
226
227 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
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 }
241
242 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
243 {
244 vinfo->v = v;
245 vinfo->s = s;
246 return (vinfo->s != NULL && vinfo->v != NULL);
247 }
248
249 SRP_VBASE *SRP_VBASE_new(char *seed_key)
250 {
251 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
252
253 if (vb == NULL)
254 return NULL;
255 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
256 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
257 OPENSSL_free(vb);
258 return NULL;
259 }
260 vb->default_g = NULL;
261 vb->default_N = NULL;
262 vb->seed_key = NULL;
263 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
264 sk_SRP_user_pwd_free(vb->users_pwd);
265 sk_SRP_gN_cache_free(vb->gN_cache);
266 OPENSSL_free(vb);
267 return NULL;
268 }
269 return vb;
270 }
271
272 void SRP_VBASE_free(SRP_VBASE *vb)
273 {
274 if (!vb)
275 return;
276 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
277 sk_SRP_gN_cache_free(vb->gN_cache);
278 OPENSSL_free(vb->seed_key);
279 OPENSSL_free(vb);
280 }
281
282 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
283 {
284 unsigned char tmp[MAX_LEN];
285 int len;
286 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
287
288 if (newgN == NULL)
289 return NULL;
290
291 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
292 goto err;
293
294 len = t_fromb64(tmp, ch);
295 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
296 return newgN;
297
298 OPENSSL_free(newgN->b64_bn);
299 err:
300 OPENSSL_free(newgN);
301 return NULL;
302 }
303
304 static void SRP_gN_free(SRP_gN_cache *gN_cache)
305 {
306 if (gN_cache == NULL)
307 return;
308 OPENSSL_free(gN_cache->b64_bn);
309 BN_free(gN_cache->bn);
310 OPENSSL_free(gN_cache);
311 }
312
313 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
314 {
315 int i;
316
317 SRP_gN *gN;
318 if (gN_tab != NULL)
319 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
320 gN = sk_SRP_gN_value(gN_tab, i);
321 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
322 return gN;
323 }
324
325 return SRP_get_default_gN(id);
326 }
327
328 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
329 {
330 int i;
331 if (gN_cache == NULL)
332 return NULL;
333
334 /* search if we have already one... */
335 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
336 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
337 if (strcmp(cache->b64_bn, ch) == 0)
338 return cache->bn;
339 }
340 { /* it is the first time that we find it */
341 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
342 if (newgN) {
343 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
344 return newgN->bn;
345 SRP_gN_free(newgN);
346 }
347 }
348 return NULL;
349 }
350
351 /*
352 * this function parses verifier file. Format is:
353 * string(index):base64(N):base64(g):0
354 * string(username):base64(v):base64(salt):int(index)
355 */
356
357 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
358 {
359 int error_code;
360 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
361 char *last_index = NULL;
362 int i;
363 char **pp;
364
365 SRP_gN *gN = NULL;
366 SRP_user_pwd *user_pwd = NULL;
367
368 TXT_DB *tmpdb = NULL;
369 BIO *in = BIO_new(BIO_s_file());
370
371 error_code = SRP_ERR_OPEN_FILE;
372
373 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
374 goto err;
375
376 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
377
378 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
379 goto err;
380
381 error_code = SRP_ERR_MEMORY;
382
383 if (vb->seed_key) {
384 last_index = SRP_get_default_gN(NULL)->id;
385 }
386 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
387 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
388 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
389 /*
390 * we add this couple in the internal Stack
391 */
392
393 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
394 goto err;
395
396 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
397 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
398 == NULL
399 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
400 == NULL
401 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
402 goto err;
403
404 gN = NULL;
405
406 if (vb->seed_key != NULL) {
407 last_index = pp[DB_srpid];
408 }
409 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
410 /* it is a user .... */
411 const SRP_gN *lgN;
412
413 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
414 error_code = SRP_ERR_MEMORY;
415 if ((user_pwd = SRP_user_pwd_new()) == NULL)
416 goto err;
417
418 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
419 if (!SRP_user_pwd_set_ids
420 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
421 goto err;
422
423 error_code = SRP_ERR_VBASE_BN_LIB;
424 if (!SRP_user_pwd_set_sv
425 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
426 goto err;
427
428 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
429 goto err;
430 user_pwd = NULL; /* abandon responsability */
431 }
432 }
433 }
434
435 if (last_index != NULL) {
436 /* this means that we want to simulate a default user */
437
438 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
439 error_code = SRP_ERR_VBASE_BN_LIB;
440 goto err;
441 }
442 vb->default_g = gN->g;
443 vb->default_N = gN->N;
444 gN = NULL;
445 }
446 error_code = SRP_NO_ERROR;
447
448 err:
449 /*
450 * there may be still some leaks to fix, if this fails, the application
451 * terminates most likely
452 */
453
454 if (gN != NULL) {
455 OPENSSL_free(gN->id);
456 OPENSSL_free(gN);
457 }
458
459 SRP_user_pwd_free(user_pwd);
460
461 TXT_DB_free(tmpdb);
462 BIO_free_all(in);
463
464 sk_SRP_gN_free(SRP_gN_tab);
465
466 return error_code;
467
468 }
469
470 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
471 {
472 int i;
473 SRP_user_pwd *user;
474 unsigned char digv[SHA_DIGEST_LENGTH];
475 unsigned char digs[SHA_DIGEST_LENGTH];
476 EVP_MD_CTX *ctxt = NULL;
477
478 if (vb == NULL)
479 return NULL;
480 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
481 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
482 if (strcmp(user->id, username) == 0)
483 return user;
484 }
485 if ((vb->seed_key == NULL) ||
486 (vb->default_g == NULL) || (vb->default_N == NULL))
487 return NULL;
488
489 /* if the user is unknown we set parameters as well if we have a seed_key */
490
491 if ((user = SRP_user_pwd_new()) == NULL)
492 return NULL;
493
494 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
495
496 if (!SRP_user_pwd_set_ids(user, username, NULL))
497 goto err;
498
499 if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
500 goto err;
501 ctxt = EVP_MD_CTX_new();
502 EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
503 EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key));
504 EVP_DigestUpdate(ctxt, username, strlen(username));
505 EVP_DigestFinal_ex(ctxt, digs, NULL);
506 EVP_MD_CTX_free(ctxt);
507 ctxt = NULL;
508 if (SRP_user_pwd_set_sv_BN(user,
509 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
510 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
511 return user;
512
513 err:
514 EVP_MD_CTX_free(ctxt);
515 SRP_user_pwd_free(user);
516 return NULL;
517 }
518
519 /*
520 * create a verifier (*salt,*verifier,g and N are in base64)
521 */
522 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
523 char **verifier, const char *N, const char *g)
524 {
525 int len;
526 char *result = NULL, *vf = NULL;
527 BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;
528 unsigned char tmp[MAX_LEN];
529 unsigned char tmp2[MAX_LEN];
530 char *defgNid = NULL;
531 int vfsize = 0;
532
533 if ((user == NULL) ||
534 (pass == NULL) || (salt == NULL) || (verifier == NULL))
535 goto err;
536
537 if (N) {
538 if ((len = t_fromb64(tmp, N)) == 0)
539 goto err;
540 N_bn = BN_bin2bn(tmp, len, NULL);
541 if ((len = t_fromb64(tmp, g)) == 0)
542 goto err;
543 g_bn = BN_bin2bn(tmp, len, NULL);
544 defgNid = "*";
545 } else {
546 SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
547 if (gN == NULL)
548 goto err;
549 N_bn = gN->N;
550 g_bn = gN->g;
551 defgNid = gN->id;
552 }
553
554 if (*salt == NULL) {
555 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
556 goto err;
557
558 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
559 } else {
560 if ((len = t_fromb64(tmp2, *salt)) == 0)
561 goto err;
562 s = BN_bin2bn(tmp2, len, NULL);
563 }
564
565 if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
566 goto err;
567
568 BN_bn2bin(v, tmp);
569 vfsize = BN_num_bytes(v) * 2;
570 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
571 goto err;
572 t_tob64(vf, tmp, BN_num_bytes(v));
573
574 if (*salt == NULL) {
575 char *tmp_salt;
576
577 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
578 goto err;
579 }
580 t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
581 *salt = tmp_salt;
582 }
583
584 *verifier = vf;
585 vf = NULL;
586 result = defgNid;
587
588 err:
589 if (N) {
590 BN_free(N_bn);
591 BN_free(g_bn);
592 }
593 OPENSSL_clear_free(vf, vfsize);
594 BN_clear_free(s);
595 BN_clear_free(v);
596 return result;
597 }
598
599 /*
600 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
601 * then the provided salt will be used. On successful exit *verifier will point
602 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
603 * provided) *salt will be populated with a newly allocated BIGNUM containing a
604 * random salt.
605 * The caller is responsible for freeing the allocated *salt and *verifier
606 * BIGNUMS.
607 */
608 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
609 BIGNUM **verifier, const BIGNUM *N,
610 const BIGNUM *g)
611 {
612 int result = 0;
613 BIGNUM *x = NULL;
614 BN_CTX *bn_ctx = BN_CTX_new();
615 unsigned char tmp2[MAX_LEN];
616 BIGNUM *salttmp = NULL;
617
618 if ((user == NULL) ||
619 (pass == NULL) ||
620 (salt == NULL) ||
621 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
622 goto err;
623
624 if (*salt == NULL) {
625 if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
626 goto err;
627
628 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
629 } else {
630 salttmp = *salt;
631 }
632
633 x = SRP_Calc_x(salttmp, user, pass);
634
635 *verifier = BN_new();
636 if (*verifier == NULL)
637 goto err;
638
639 if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
640 BN_clear_free(*verifier);
641 goto err;
642 }
643
644 result = 1;
645 *salt = salttmp;
646
647 err:
648 if (salt != NULL && *salt != salttmp)
649 BN_clear_free(salttmp);
650 BN_clear_free(x);
651 BN_CTX_free(bn_ctx);
652 return result;
653 }
654
655 #endif