check return values for EVP_Digest*() APIs
[openssl.git] / crypto / srp / srp_vfy.c
1 /*
2  * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OPENSSL_NO_SRP
11 # include "internal/cryptlib.h"
12 # include <openssl/sha.h>
13 # include <openssl/srp.h>
14 # include <openssl/evp.h>
15 # include <openssl/buffer.h>
16 # include <openssl/rand.h>
17 # include <openssl/txt_db.h>
18
19 # define SRP_RANDOM_SALT_LEN 20
20 # define MAX_LEN 2500
21
22 static char b64table[] =
23     "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
24
25 /*
26  * the following two conversion routines have been inspired by code from
27  * Stanford
28  */
29
30 /*
31  * Convert a base64 string into raw byte array representation.
32  */
33 static int t_fromb64(unsigned char *a, const char *src)
34 {
35     char *loc;
36     int i, j;
37     int size;
38
39     while (*src && (*src == ' ' || *src == '\t' || *src == '\n'))
40         ++src;
41     size = strlen(src);
42     i = 0;
43     while (i < size) {
44         loc = strchr(b64table, src[i]);
45         if (loc == (char *)0)
46             break;
47         else
48             a[i] = loc - b64table;
49         ++i;
50     }
51     /* if nothing valid to process we have a zero length response */
52     if (i == 0)
53         return 0;
54     size = i;
55     i = size - 1;
56     j = size;
57     while (1) {
58         a[j] = a[i];
59         if (--i < 0)
60             break;
61         a[j] |= (a[i] & 3) << 6;
62         --j;
63         a[j] = (unsigned char)((a[i] & 0x3c) >> 2);
64         if (--i < 0)
65             break;
66         a[j] |= (a[i] & 0xf) << 4;
67         --j;
68         a[j] = (unsigned char)((a[i] & 0x30) >> 4);
69         if (--i < 0)
70             break;
71         a[j] |= (a[i] << 2);
72
73         a[--j] = 0;
74         if (--i < 0)
75             break;
76     }
77     while (a[j] == 0 && j <= size)
78         ++j;
79     i = 0;
80     while (j <= size)
81         a[i++] = a[j++];
82     return i;
83 }
84
85 /*
86  * Convert a raw byte string into a null-terminated base64 ASCII string.
87  */
88 static char *t_tob64(char *dst, const unsigned char *src, int size)
89 {
90     int c, pos = size % 3;
91     unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
92     char *olddst = dst;
93
94     switch (pos) {
95     case 1:
96         b2 = src[0];
97         break;
98     case 2:
99         b1 = src[0];
100         b2 = src[1];
101         break;
102     }
103
104     while (1) {
105         c = (b0 & 0xfc) >> 2;
106         if (notleading || c != 0) {
107             *dst++ = b64table[c];
108             notleading = 1;
109         }
110         c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
111         if (notleading || c != 0) {
112             *dst++ = b64table[c];
113             notleading = 1;
114         }
115         c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
116         if (notleading || c != 0) {
117             *dst++ = b64table[c];
118             notleading = 1;
119         }
120         c = b2 & 0x3f;
121         if (notleading || c != 0) {
122             *dst++ = b64table[c];
123             notleading = 1;
124         }
125         if (pos >= size)
126             break;
127         else {
128             b0 = src[pos++];
129             b1 = src[pos++];
130             b2 = src[pos++];
131         }
132     }
133
134     *dst++ = '\0';
135     return olddst;
136 }
137
138 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
139 {
140     if (user_pwd == NULL)
141         return;
142     BN_free(user_pwd->s);
143     BN_clear_free(user_pwd->v);
144     OPENSSL_free(user_pwd->id);
145     OPENSSL_free(user_pwd->info);
146     OPENSSL_free(user_pwd);
147 }
148
149 static SRP_user_pwd *SRP_user_pwd_new(void)
150 {
151     SRP_user_pwd *ret = OPENSSL_malloc(sizeof(*ret));
152     if (ret == NULL)
153         return NULL;
154     ret->N = NULL;
155     ret->g = NULL;
156     ret->s = NULL;
157     ret->v = NULL;
158     ret->id = NULL;
159     ret->info = NULL;
160     return ret;
161 }
162
163 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
164                                 const BIGNUM *N)
165 {
166     vinfo->N = N;
167     vinfo->g = g;
168 }
169
170 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
171                                 const char *info)
172 {
173     if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
174         return 0;
175     return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
176 }
177
178 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
179                                const char *v)
180 {
181     unsigned char tmp[MAX_LEN];
182     int len;
183
184     if (strlen(s) > MAX_LEN || strlen(v) > MAX_LEN)
185         return 0;
186     len = t_fromb64(tmp, v);
187     if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
188         return 0;
189     len = t_fromb64(tmp, s);
190     return ((vinfo->s = BN_bin2bn(tmp, len, NULL)) != NULL);
191 }
192
193 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
194 {
195     vinfo->v = v;
196     vinfo->s = s;
197     return (vinfo->s != NULL && vinfo->v != NULL);
198 }
199
200 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
201 {
202     SRP_user_pwd *ret;
203
204     if (src == NULL)
205         return NULL;
206     if ((ret = SRP_user_pwd_new()) == NULL)
207         return NULL;
208
209     SRP_user_pwd_set_gN(ret, src->g, src->N);
210     if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
211         || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
212             SRP_user_pwd_free(ret);
213             return NULL;
214     }
215     return ret;
216 }
217
218 SRP_VBASE *SRP_VBASE_new(char *seed_key)
219 {
220     SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
221
222     if (vb == NULL)
223         return NULL;
224     if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
225         || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
226         OPENSSL_free(vb);
227         return NULL;
228     }
229     vb->default_g = NULL;
230     vb->default_N = NULL;
231     vb->seed_key = NULL;
232     if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
233         sk_SRP_user_pwd_free(vb->users_pwd);
234         sk_SRP_gN_cache_free(vb->gN_cache);
235         OPENSSL_free(vb);
236         return NULL;
237     }
238     return vb;
239 }
240
241 void SRP_VBASE_free(SRP_VBASE *vb)
242 {
243     if (!vb)
244         return;
245     sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
246     sk_SRP_gN_cache_free(vb->gN_cache);
247     OPENSSL_free(vb->seed_key);
248     OPENSSL_free(vb);
249 }
250
251 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
252 {
253     unsigned char tmp[MAX_LEN];
254     int len;
255     SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
256
257     if (newgN == NULL)
258         return NULL;
259
260     if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
261         goto err;
262
263     len = t_fromb64(tmp, ch);
264     if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
265         return newgN;
266
267     OPENSSL_free(newgN->b64_bn);
268  err:
269     OPENSSL_free(newgN);
270     return NULL;
271 }
272
273 static void SRP_gN_free(SRP_gN_cache *gN_cache)
274 {
275     if (gN_cache == NULL)
276         return;
277     OPENSSL_free(gN_cache->b64_bn);
278     BN_free(gN_cache->bn);
279     OPENSSL_free(gN_cache);
280 }
281
282 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
283 {
284     int i;
285
286     SRP_gN *gN;
287     if (gN_tab != NULL)
288         for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
289             gN = sk_SRP_gN_value(gN_tab, i);
290             if (gN && (id == NULL || strcmp(gN->id, id) == 0))
291                 return gN;
292         }
293
294     return SRP_get_default_gN(id);
295 }
296
297 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
298 {
299     int i;
300     if (gN_cache == NULL)
301         return NULL;
302
303     /* search if we have already one... */
304     for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
305         SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
306         if (strcmp(cache->b64_bn, ch) == 0)
307             return cache->bn;
308     }
309     {                           /* it is the first time that we find it */
310         SRP_gN_cache *newgN = SRP_gN_new_init(ch);
311         if (newgN) {
312             if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
313                 return newgN->bn;
314             SRP_gN_free(newgN);
315         }
316     }
317     return NULL;
318 }
319
320 /*
321  * this function parses verifier file. Format is:
322  * string(index):base64(N):base64(g):0
323  * string(username):base64(v):base64(salt):int(index)
324  */
325
326 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
327 {
328     int error_code;
329     STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
330     char *last_index = NULL;
331     int i;
332     char **pp;
333
334     SRP_gN *gN = NULL;
335     SRP_user_pwd *user_pwd = NULL;
336
337     TXT_DB *tmpdb = NULL;
338     BIO *in = BIO_new(BIO_s_file());
339
340     error_code = SRP_ERR_OPEN_FILE;
341
342     if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
343         goto err;
344
345     error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
346
347     if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
348         goto err;
349
350     error_code = SRP_ERR_MEMORY;
351
352     if (vb->seed_key) {
353         last_index = SRP_get_default_gN(NULL)->id;
354     }
355     for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
356         pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
357         if (pp[DB_srptype][0] == DB_SRP_INDEX) {
358             /*
359              * we add this couple in the internal Stack
360              */
361
362             if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
363                 goto err;
364
365             if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
366                 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
367                         == NULL
368                 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
369                         == NULL
370                 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
371                 goto err;
372
373             gN = NULL;
374
375             if (vb->seed_key != NULL) {
376                 last_index = pp[DB_srpid];
377             }
378         } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
379             /* it is a user .... */
380             const SRP_gN *lgN;
381
382             if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
383                 error_code = SRP_ERR_MEMORY;
384                 if ((user_pwd = SRP_user_pwd_new()) == NULL)
385                     goto err;
386
387                 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
388                 if (!SRP_user_pwd_set_ids
389                     (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
390                     goto err;
391
392                 error_code = SRP_ERR_VBASE_BN_LIB;
393                 if (!SRP_user_pwd_set_sv
394                     (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
395                     goto err;
396
397                 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
398                     goto err;
399                 user_pwd = NULL; /* abandon responsibility */
400             }
401         }
402     }
403
404     if (last_index != NULL) {
405         /* this means that we want to simulate a default user */
406
407         if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
408             error_code = SRP_ERR_VBASE_BN_LIB;
409             goto err;
410         }
411         vb->default_g = gN->g;
412         vb->default_N = gN->N;
413         gN = NULL;
414     }
415     error_code = SRP_NO_ERROR;
416
417  err:
418     /*
419      * there may be still some leaks to fix, if this fails, the application
420      * terminates most likely
421      */
422
423     if (gN != NULL) {
424         OPENSSL_free(gN->id);
425         OPENSSL_free(gN);
426     }
427
428     SRP_user_pwd_free(user_pwd);
429
430     TXT_DB_free(tmpdb);
431     BIO_free_all(in);
432
433     sk_SRP_gN_free(SRP_gN_tab);
434
435     return error_code;
436
437 }
438
439 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
440 {
441     int i;
442     SRP_user_pwd *user;
443
444     if (vb == NULL)
445         return NULL;
446
447     for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
448         user = sk_SRP_user_pwd_value(vb->users_pwd, i);
449         if (strcmp(user->id, username) == 0)
450             return user;
451     }
452
453     return NULL;
454 }
455
456  #if OPENSSL_API_COMPAT < 0x10100000L
457 /*
458  * DEPRECATED: use SRP_VBASE_get1_by_user instead.
459  * This method ignores the configured seed and fails for an unknown user.
460  * Ownership of the returned pointer is not released to the caller.
461  * In other words, caller must not free the result.
462  */
463 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
464 {
465     return find_user(vb, username);
466 }
467 #endif
468
469 /*
470  * Ownership of the returned pointer is released to the caller.
471  * In other words, caller must free the result once done.
472  */
473 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
474 {
475     SRP_user_pwd *user;
476     unsigned char digv[SHA_DIGEST_LENGTH];
477     unsigned char digs[SHA_DIGEST_LENGTH];
478     EVP_MD_CTX *ctxt = NULL;
479
480     if (vb == NULL)
481         return NULL;
482
483     if ((user = find_user(vb, username)) != NULL)
484         return srp_user_pwd_dup(user);
485
486     if ((vb->seed_key == NULL) ||
487         (vb->default_g == NULL) || (vb->default_N == NULL))
488         return NULL;
489
490 /* if the user is unknown we set parameters as well if we have a seed_key */
491
492     if ((user = SRP_user_pwd_new()) == NULL)
493         return NULL;
494
495     SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
496
497     if (!SRP_user_pwd_set_ids(user, username, NULL))
498         goto err;
499
500     if (RAND_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
501         goto err;
502     ctxt = EVP_MD_CTX_new();
503     if (ctxt == NULL
504         || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
505         || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
506         || !EVP_DigestUpdate(ctxt, username, strlen(username))
507         || !EVP_DigestFinal_ex(ctxt, digs, NULL))
508         goto err;
509     EVP_MD_CTX_free(ctxt);
510     ctxt = NULL;
511     if (SRP_user_pwd_set_sv_BN(user,
512                                BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
513                                BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
514         return user;
515
516  err:
517     EVP_MD_CTX_free(ctxt);
518     SRP_user_pwd_free(user);
519     return NULL;
520 }
521
522 /*
523  * create a verifier (*salt,*verifier,g and N are in base64)
524  */
525 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
526                           char **verifier, const char *N, const char *g)
527 {
528     int len;
529     char *result = NULL, *vf = NULL;
530     const BIGNUM *N_bn = NULL, *g_bn = NULL;
531     BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
532     unsigned char tmp[MAX_LEN];
533     unsigned char tmp2[MAX_LEN];
534     char *defgNid = NULL;
535     int vfsize = 0;
536
537     if ((user == NULL) ||
538         (pass == NULL) || (salt == NULL) || (verifier == NULL))
539         goto err;
540
541     if (N) {
542         if ((len = t_fromb64(tmp, N)) == 0)
543             goto err;
544         N_bn_alloc = BN_bin2bn(tmp, len, NULL);
545         N_bn = N_bn_alloc;
546         if ((len = t_fromb64(tmp, g)) == 0)
547             goto err;
548         g_bn_alloc = BN_bin2bn(tmp, len, NULL);
549         g_bn = g_bn_alloc;
550         defgNid = "*";
551     } else {
552         SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
553         if (gN == NULL)
554             goto err;
555         N_bn = gN->N;
556         g_bn = gN->g;
557         defgNid = gN->id;
558     }
559
560     if (*salt == NULL) {
561         if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
562             goto err;
563
564         s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
565     } else {
566         if ((len = t_fromb64(tmp2, *salt)) == 0)
567             goto err;
568         s = BN_bin2bn(tmp2, len, NULL);
569     }
570
571     if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
572         goto err;
573
574     BN_bn2bin(v, tmp);
575     vfsize = BN_num_bytes(v) * 2;
576     if (((vf = OPENSSL_malloc(vfsize)) == NULL))
577         goto err;
578     t_tob64(vf, tmp, BN_num_bytes(v));
579
580     if (*salt == NULL) {
581         char *tmp_salt;
582
583         if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
584             goto err;
585         }
586         t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
587         *salt = tmp_salt;
588     }
589
590     *verifier = vf;
591     vf = NULL;
592     result = defgNid;
593
594  err:
595     BN_free(N_bn_alloc);
596     BN_free(g_bn_alloc);
597     OPENSSL_clear_free(vf, vfsize);
598     BN_clear_free(s);
599     BN_clear_free(v);
600     return result;
601 }
602
603 /*
604  * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
605  * then the provided salt will be used. On successful exit *verifier will point
606  * to a newly allocated BIGNUM containing the verifier and (if a salt was not
607  * provided) *salt will be populated with a newly allocated BIGNUM containing a
608  * random salt.
609  * The caller is responsible for freeing the allocated *salt and *verifier
610  * BIGNUMS.
611  */
612 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
613                            BIGNUM **verifier, const BIGNUM *N,
614                            const BIGNUM *g)
615 {
616     int result = 0;
617     BIGNUM *x = NULL;
618     BN_CTX *bn_ctx = BN_CTX_new();
619     unsigned char tmp2[MAX_LEN];
620     BIGNUM *salttmp = NULL;
621
622     if ((user == NULL) ||
623         (pass == NULL) ||
624         (salt == NULL) ||
625         (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
626         goto err;
627
628     if (*salt == NULL) {
629         if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)
630             goto err;
631
632         salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
633     } else {
634         salttmp = *salt;
635     }
636
637     x = SRP_Calc_x(salttmp, user, pass);
638
639     *verifier = BN_new();
640     if (*verifier == NULL)
641         goto err;
642
643     if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
644         BN_clear_free(*verifier);
645         goto err;
646     }
647
648     result = 1;
649     *salt = salttmp;
650
651  err:
652     if (salt != NULL && *salt != salttmp)
653         BN_clear_free(salttmp);
654     BN_clear_free(x);
655     BN_CTX_free(bn_ctx);
656     return result;
657 }
658
659 #endif