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