f0ed6da6f6fd7c203aab490bc915fb25ab8d7069
[openssl.git] / crypto / srp / srp_vfy.c
1 /*
2  * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  *
10  * Originally written by Christophe Renou and Peter Sylvester,
11  * for the EdelKey project.
12  */
13
14 #ifndef OPENSSL_NO_SRP
15 # include "internal/cryptlib.h"
16 # include "crypto/evp.h"
17 # include <openssl/sha.h>
18 # include <openssl/srp.h>
19 # include <openssl/evp.h>
20 # include <openssl/buffer.h>
21 # include <openssl/rand.h>
22 # include <openssl/txt_db.h>
23 # include <openssl/err.h>
24
25 # define SRP_RANDOM_SALT_LEN 20
26 # define MAX_LEN 2500
27
28 DEFINE_STACK_OF(SRP_user_pwd)
29 DEFINE_STACK_OF(SRP_gN_cache)
30 DEFINE_STACK_OF(SRP_gN)
31
32 /*
33  * Note that SRP uses its own variant of base 64 encoding. A different base64
34  * alphabet is used and no padding '=' characters are added. Instead we pad to
35  * the front with 0 bytes and subsequently strip off leading encoded padding.
36  * This variant is used for compatibility with other SRP implementations -
37  * notably libsrp, but also others. It is also required for backwards
38  * compatibility in order to load verifier files from other OpenSSL versions.
39  */
40
41 /*
42  * Convert a base64 string into raw byte array representation.
43  * Returns the length of the decoded data, or -1 on error.
44  */
45 static int t_fromb64(unsigned char *a, size_t alen, const char *src)
46 {
47     EVP_ENCODE_CTX *ctx;
48     int outl = 0, outl2 = 0;
49     size_t size, padsize;
50     const unsigned char *pad = (const unsigned char *)"00";
51
52     while (*src == ' ' || *src == '\t' || *src == '\n')
53         ++src;
54     size = strlen(src);
55     padsize = 4 - (size & 3);
56     padsize &= 3;
57
58     /* Four bytes in src become three bytes output. */
59     if (size > INT_MAX || ((size + padsize) / 4) * 3 > alen)
60         return -1;
61
62     ctx = EVP_ENCODE_CTX_new();
63     if (ctx == NULL)
64         return -1;
65
66     /*
67      * This should never occur because 1 byte of data always requires 2 bytes of
68      * encoding, i.e.
69      *  0 bytes unencoded = 0 bytes encoded
70      *  1 byte unencoded  = 2 bytes encoded
71      *  2 bytes unencoded = 3 bytes encoded
72      *  3 bytes unencoded = 4 bytes encoded
73      *  4 bytes unencoded = 6 bytes encoded
74      *  etc
75      */
76     if (padsize == 3) {
77         outl = -1;
78         goto err;
79     }
80
81     /* Valid padsize values are now 0, 1 or 2 */
82
83     EVP_DecodeInit(ctx);
84     evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_USE_SRP_ALPHABET);
85
86     /* Add any encoded padding that is required */
87     if (padsize != 0
88             && EVP_DecodeUpdate(ctx, a, &outl, pad, padsize) < 0) {
89         outl = -1;
90         goto err;
91     }
92     if (EVP_DecodeUpdate(ctx, a, &outl2, (const unsigned char *)src, size) < 0) {
93         outl = -1;
94         goto err;
95     }
96     outl += outl2;
97     EVP_DecodeFinal(ctx, a + outl, &outl2);
98     outl += outl2;
99
100     /* Strip off the leading padding */
101     if (padsize != 0) {
102         if ((int)padsize >= outl) {
103             outl = -1;
104             goto err;
105         }
106
107         /*
108          * If we added 1 byte of padding prior to encoding then we have 2 bytes
109          * of "real" data which gets spread across 4 encoded bytes like this:
110          *   (6 bits pad)(2 bits pad | 4 bits data)(6 bits data)(6 bits data)
111          * So 1 byte of pre-encoding padding results in 1 full byte of encoded
112          * padding.
113          * If we added 2 bytes of padding prior to encoding this gets encoded
114          * as:
115          *   (6 bits pad)(6 bits pad)(4 bits pad | 2 bits data)(6 bits data)
116          * So 2 bytes of pre-encoding padding results in 2 full bytes of encoded
117          * padding, i.e. we have to strip the same number of bytes of padding
118          * from the encoded data as we added to the pre-encoded data.
119          */
120         memmove(a, a + padsize, outl - padsize);
121         outl -= padsize;
122     }
123
124  err:
125     EVP_ENCODE_CTX_free(ctx);
126
127     return outl;
128 }
129
130 /*
131  * Convert a raw byte string into a null-terminated base64 ASCII string.
132  * Returns 1 on success or 0 on error.
133  */
134 static int t_tob64(char *dst, const unsigned char *src, int size)
135 {
136     EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
137     int outl = 0, outl2 = 0;
138     unsigned char pad[2] = {0, 0};
139     size_t leadz = 0;
140
141     if (ctx == NULL)
142         return 0;
143
144     EVP_EncodeInit(ctx);
145     evp_encode_ctx_set_flags(ctx, EVP_ENCODE_CTX_NO_NEWLINES
146                                   | EVP_ENCODE_CTX_USE_SRP_ALPHABET);
147
148     /*
149      * We pad at the front with zero bytes until the length is a multiple of 3
150      * so that EVP_EncodeUpdate/EVP_EncodeFinal does not add any of its own "="
151      * padding
152      */
153     leadz = 3 - (size % 3);
154     if (leadz != 3
155             && !EVP_EncodeUpdate(ctx, (unsigned char *)dst, &outl, pad,
156                                  leadz)) {
157         EVP_ENCODE_CTX_free(ctx);
158         return 0;
159     }
160
161     if (!EVP_EncodeUpdate(ctx, (unsigned char *)dst + outl, &outl2, src,
162                           size)) {
163         EVP_ENCODE_CTX_free(ctx);
164         return 0;
165     }
166     outl += outl2;
167     EVP_EncodeFinal(ctx, (unsigned char *)dst + outl, &outl2);
168     outl += outl2;
169
170     /* Strip the encoded padding at the front */
171     if (leadz != 3) {
172         memmove(dst, dst + leadz, outl - leadz);
173         dst[outl - leadz] = '\0';
174     }
175
176     EVP_ENCODE_CTX_free(ctx);
177     return 1;
178 }
179
180 void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
181 {
182     if (user_pwd == NULL)
183         return;
184     BN_free(user_pwd->s);
185     BN_clear_free(user_pwd->v);
186     OPENSSL_free(user_pwd->id);
187     OPENSSL_free(user_pwd->info);
188     OPENSSL_free(user_pwd);
189 }
190
191 SRP_user_pwd *SRP_user_pwd_new(void)
192 {
193     SRP_user_pwd *ret;
194
195     if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
196         /* SRPerr(SRP_F_SRP_USER_PWD_NEW, ERR_R_MALLOC_FAILURE); */ /*ckerr_ignore*/
197         return NULL;
198     }
199     ret->N = NULL;
200     ret->g = NULL;
201     ret->s = NULL;
202     ret->v = NULL;
203     ret->id = NULL;
204     ret->info = NULL;
205     return ret;
206 }
207
208 void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
209                          const BIGNUM *N)
210 {
211     vinfo->N = N;
212     vinfo->g = g;
213 }
214
215 int SRP_user_pwd_set1_ids(SRP_user_pwd *vinfo, const char *id,
216                           const char *info)
217 {
218     OPENSSL_free(vinfo->id);
219     OPENSSL_free(vinfo->info);
220     if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
221         return 0;
222     return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
223 }
224
225 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
226                                const char *v)
227 {
228     unsigned char tmp[MAX_LEN];
229     int len;
230
231     vinfo->v = NULL;
232     vinfo->s = NULL;
233
234     len = t_fromb64(tmp, sizeof(tmp), v);
235     if (len < 0)
236         return 0;
237     if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
238         return 0;
239     len = t_fromb64(tmp, sizeof(tmp), s);
240     if (len < 0)
241         goto err;
242     vinfo->s = BN_bin2bn(tmp, len, NULL);
243     if (vinfo->s == NULL)
244         goto err;
245     return 1;
246  err:
247     BN_free(vinfo->v);
248     vinfo->v = NULL;
249     return 0;
250 }
251
252 int SRP_user_pwd_set0_sv(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
253 {
254     BN_free(vinfo->s);
255     BN_clear_free(vinfo->v);
256     vinfo->v = v;
257     vinfo->s = s;
258     return (vinfo->s != NULL && vinfo->v != NULL);
259 }
260
261 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
262 {
263     SRP_user_pwd *ret;
264
265     if (src == NULL)
266         return NULL;
267     if ((ret = SRP_user_pwd_new()) == NULL)
268         return NULL;
269
270     SRP_user_pwd_set_gN(ret, src->g, src->N);
271     if (!SRP_user_pwd_set1_ids(ret, src->id, src->info)
272         || !SRP_user_pwd_set0_sv(ret, BN_dup(src->s), BN_dup(src->v))) {
273             SRP_user_pwd_free(ret);
274             return NULL;
275     }
276     return ret;
277 }
278
279 SRP_VBASE *SRP_VBASE_new(char *seed_key)
280 {
281     SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
282
283     if (vb == NULL)
284         return NULL;
285     if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
286         || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
287         OPENSSL_free(vb);
288         return NULL;
289     }
290     vb->default_g = NULL;
291     vb->default_N = NULL;
292     vb->seed_key = NULL;
293     if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
294         sk_SRP_user_pwd_free(vb->users_pwd);
295         sk_SRP_gN_cache_free(vb->gN_cache);
296         OPENSSL_free(vb);
297         return NULL;
298     }
299     return vb;
300 }
301
302 void SRP_VBASE_free(SRP_VBASE *vb)
303 {
304     if (!vb)
305         return;
306     sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
307     sk_SRP_gN_cache_free(vb->gN_cache);
308     OPENSSL_free(vb->seed_key);
309     OPENSSL_free(vb);
310 }
311
312 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
313 {
314     unsigned char tmp[MAX_LEN];
315     int len;
316     SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
317
318     if (newgN == NULL)
319         return NULL;
320
321     len = t_fromb64(tmp, sizeof(tmp), ch);
322     if (len < 0)
323         goto err;
324
325     if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
326         goto err;
327
328     if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
329         return newgN;
330
331     OPENSSL_free(newgN->b64_bn);
332  err:
333     OPENSSL_free(newgN);
334     return NULL;
335 }
336
337 static void SRP_gN_free(SRP_gN_cache *gN_cache)
338 {
339     if (gN_cache == NULL)
340         return;
341     OPENSSL_free(gN_cache->b64_bn);
342     BN_free(gN_cache->bn);
343     OPENSSL_free(gN_cache);
344 }
345
346 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
347 {
348     int i;
349
350     SRP_gN *gN;
351     if (gN_tab != NULL) {
352         for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
353             gN = sk_SRP_gN_value(gN_tab, i);
354             if (gN && (id == NULL || strcmp(gN->id, id) == 0))
355                 return gN;
356         }
357     }
358
359     return SRP_get_default_gN(id);
360 }
361
362 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
363 {
364     int i;
365     if (gN_cache == NULL)
366         return NULL;
367
368     /* search if we have already one... */
369     for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
370         SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
371         if (strcmp(cache->b64_bn, ch) == 0)
372             return cache->bn;
373     }
374     {                           /* it is the first time that we find it */
375         SRP_gN_cache *newgN = SRP_gN_new_init(ch);
376         if (newgN) {
377             if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
378                 return newgN->bn;
379             SRP_gN_free(newgN);
380         }
381     }
382     return NULL;
383 }
384
385 /*
386  * This function parses the verifier file generated by the srp app.
387  * The format for each entry is:
388  * V base64(verifier) base64(salt) username gNid userinfo(optional)
389  * or
390  * I base64(N) base64(g)
391  * Note that base64 is the SRP variant of base64 encoding described
392  * in t_fromb64().
393  */
394
395 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
396 {
397     int error_code;
398     STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
399     char *last_index = NULL;
400     int i;
401     char **pp;
402
403     SRP_gN *gN = NULL;
404     SRP_user_pwd *user_pwd = NULL;
405
406     TXT_DB *tmpdb = NULL;
407     BIO *in = BIO_new(BIO_s_file());
408
409     error_code = SRP_ERR_OPEN_FILE;
410
411     if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
412         goto err;
413
414     error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
415
416     if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
417         goto err;
418
419     error_code = SRP_ERR_MEMORY;
420
421     if (vb->seed_key) {
422         last_index = SRP_get_default_gN(NULL)->id;
423     }
424     for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
425         pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
426         if (pp[DB_srptype][0] == DB_SRP_INDEX) {
427             /*
428              * we add this couple in the internal Stack
429              */
430
431             if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
432                 goto err;
433
434             if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
435                 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
436                         == NULL
437                 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
438                         == NULL
439                 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
440                 goto err;
441
442             gN = NULL;
443
444             if (vb->seed_key != NULL) {
445                 last_index = pp[DB_srpid];
446             }
447         } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
448             /* it is a user .... */
449             const SRP_gN *lgN;
450
451             if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
452                 error_code = SRP_ERR_MEMORY;
453                 if ((user_pwd = SRP_user_pwd_new()) == NULL)
454                     goto err;
455
456                 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
457                 if (!SRP_user_pwd_set1_ids
458                     (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
459                     goto err;
460
461                 error_code = SRP_ERR_VBASE_BN_LIB;
462                 if (!SRP_user_pwd_set_sv
463                     (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
464                     goto err;
465
466                 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
467                     goto err;
468                 user_pwd = NULL; /* abandon responsibility */
469             }
470         }
471     }
472
473     if (last_index != NULL) {
474         /* this means that we want to simulate a default user */
475
476         if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
477             error_code = SRP_ERR_VBASE_BN_LIB;
478             goto err;
479         }
480         vb->default_g = gN->g;
481         vb->default_N = gN->N;
482         gN = NULL;
483     }
484     error_code = SRP_NO_ERROR;
485
486  err:
487     /*
488      * there may be still some leaks to fix, if this fails, the application
489      * terminates most likely
490      */
491
492     if (gN != NULL) {
493         OPENSSL_free(gN->id);
494         OPENSSL_free(gN);
495     }
496
497     SRP_user_pwd_free(user_pwd);
498
499     TXT_DB_free(tmpdb);
500     BIO_free_all(in);
501
502     sk_SRP_gN_free(SRP_gN_tab);
503
504     return error_code;
505
506 }
507
508 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
509 {
510     int i;
511     SRP_user_pwd *user;
512
513     if (vb == NULL)
514         return NULL;
515
516     for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
517         user = sk_SRP_user_pwd_value(vb->users_pwd, i);
518         if (strcmp(user->id, username) == 0)
519             return user;
520     }
521
522     return NULL;
523 }
524
525 int SRP_VBASE_add0_user(SRP_VBASE *vb, SRP_user_pwd *user_pwd)
526 {
527     if (sk_SRP_user_pwd_push(vb->users_pwd, user_pwd) <= 0)
528         return 0;
529     return 1;
530 }
531
532 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
533 /*
534  * DEPRECATED: use SRP_VBASE_get1_by_user instead.
535  * This method ignores the configured seed and fails for an unknown user.
536  * Ownership of the returned pointer is not released to the caller.
537  * In other words, caller must not free the result.
538  */
539 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
540 {
541     return find_user(vb, username);
542 }
543 # endif
544
545 /*
546  * Ownership of the returned pointer is released to the caller.
547  * In other words, caller must free the result once done.
548  */
549 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
550 {
551     SRP_user_pwd *user;
552     unsigned char digv[SHA_DIGEST_LENGTH];
553     unsigned char digs[SHA_DIGEST_LENGTH];
554     EVP_MD_CTX *ctxt = NULL;
555
556     if (vb == NULL)
557         return NULL;
558
559     if ((user = find_user(vb, username)) != NULL)
560         return srp_user_pwd_dup(user);
561
562     if ((vb->seed_key == NULL) ||
563         (vb->default_g == NULL) || (vb->default_N == NULL))
564         return NULL;
565
566 /* if the user is unknown we set parameters as well if we have a seed_key */
567
568     if ((user = SRP_user_pwd_new()) == NULL)
569         return NULL;
570
571     SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
572
573     if (!SRP_user_pwd_set1_ids(user, username, NULL))
574         goto err;
575
576     if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
577         goto err;
578     ctxt = EVP_MD_CTX_new();
579     if (ctxt == NULL
580         || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
581         || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
582         || !EVP_DigestUpdate(ctxt, username, strlen(username))
583         || !EVP_DigestFinal_ex(ctxt, digs, NULL))
584         goto err;
585     EVP_MD_CTX_free(ctxt);
586     ctxt = NULL;
587     if (SRP_user_pwd_set0_sv(user,
588                                BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
589                                BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
590         return user;
591
592  err:
593     EVP_MD_CTX_free(ctxt);
594     SRP_user_pwd_free(user);
595     return NULL;
596 }
597
598 /*
599  * create a verifier (*salt,*verifier,g and N are in base64)
600  */
601 char *SRP_create_verifier_ex(const char *user, const char *pass, char **salt,
602                              char **verifier, const char *N, const char *g,
603                              OPENSSL_CTX *libctx, const char *propq)
604 {
605     int len;
606     char *result = NULL, *vf = NULL;
607     const BIGNUM *N_bn = NULL, *g_bn = NULL;
608     BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
609     unsigned char tmp[MAX_LEN];
610     unsigned char tmp2[MAX_LEN];
611     char *defgNid = NULL;
612     int vfsize = 0;
613
614     if ((user == NULL) ||
615         (pass == NULL) || (salt == NULL) || (verifier == NULL))
616         goto err;
617
618     if (N) {
619         if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
620             goto err;
621         N_bn_alloc = BN_bin2bn(tmp, len, NULL);
622         if (N_bn_alloc == NULL)
623             goto err;
624         N_bn = N_bn_alloc;
625         if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)
626             goto err;
627         g_bn_alloc = BN_bin2bn(tmp, len, NULL);
628         if (g_bn_alloc == NULL)
629             goto err;
630         g_bn = g_bn_alloc;
631         defgNid = "*";
632     } else {
633         SRP_gN *gN = SRP_get_default_gN(g);
634         if (gN == NULL)
635             goto err;
636         N_bn = gN->N;
637         g_bn = gN->g;
638         defgNid = gN->id;
639     }
640
641     if (*salt == NULL) {
642         if (RAND_bytes_ex(libctx, tmp2, SRP_RANDOM_SALT_LEN) <= 0)
643             goto err;
644
645         s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
646     } else {
647         if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
648             goto err;
649         s = BN_bin2bn(tmp2, len, NULL);
650     }
651     if (s == NULL)
652         goto err;
653
654     if (!SRP_create_verifier_BN_ex(user, pass, &s, &v, N_bn, g_bn, libctx,
655                                    propq))
656         goto err;
657
658     if (BN_bn2bin(v, tmp) < 0)
659         goto err;
660     vfsize = BN_num_bytes(v) * 2;
661     if (((vf = OPENSSL_malloc(vfsize)) == NULL))
662         goto err;
663     if (!t_tob64(vf, tmp, BN_num_bytes(v)))
664         goto err;
665
666     if (*salt == NULL) {
667         char *tmp_salt;
668
669         if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
670             goto err;
671         }
672         if (!t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN)) {
673             OPENSSL_free(tmp_salt);
674             goto err;
675         }
676         *salt = tmp_salt;
677     }
678
679     *verifier = vf;
680     vf = NULL;
681     result = defgNid;
682
683  err:
684     BN_free(N_bn_alloc);
685     BN_free(g_bn_alloc);
686     OPENSSL_clear_free(vf, vfsize);
687     BN_clear_free(s);
688     BN_clear_free(v);
689     return result;
690 }
691
692 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
693                           char **verifier, const char *N, const char *g)
694 {
695     return SRP_create_verifier_ex(user, pass, salt, verifier, N, g, NULL, NULL);
696 }
697
698 /*
699  * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
700  * then the provided salt will be used. On successful exit *verifier will point
701  * to a newly allocated BIGNUM containing the verifier and (if a salt was not
702  * provided) *salt will be populated with a newly allocated BIGNUM containing a
703  * random salt.
704  * The caller is responsible for freeing the allocated *salt and *verifier
705  * BIGNUMS.
706  */
707 int SRP_create_verifier_BN_ex(const char *user, const char *pass, BIGNUM **salt,
708                               BIGNUM **verifier, const BIGNUM *N,
709                               const BIGNUM *g, OPENSSL_CTX *libctx,
710                               const char *propq)
711 {
712     int result = 0;
713     BIGNUM *x = NULL;
714     BN_CTX *bn_ctx = BN_CTX_new_ex(libctx);
715     unsigned char tmp2[MAX_LEN];
716     BIGNUM *salttmp = NULL;
717
718     if ((user == NULL) ||
719         (pass == NULL) ||
720         (salt == NULL) ||
721         (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
722         goto err;
723
724     if (*salt == NULL) {
725         if (RAND_bytes_ex(libctx, tmp2, SRP_RANDOM_SALT_LEN) <= 0)
726             goto err;
727
728         salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
729         if (salttmp == NULL)
730             goto err;
731     } else {
732         salttmp = *salt;
733     }
734
735     x = SRP_Calc_x_ex(salttmp, user, pass, libctx, propq);
736     if (x == NULL)
737         goto err;
738
739     *verifier = BN_new();
740     if (*verifier == NULL)
741         goto err;
742
743     if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
744         BN_clear_free(*verifier);
745         goto err;
746     }
747
748     result = 1;
749     *salt = salttmp;
750
751  err:
752     if (salt != NULL && *salt != salttmp)
753         BN_clear_free(salttmp);
754     BN_clear_free(x);
755     BN_CTX_free(bn_ctx);
756     return result;
757 }
758
759 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
760                            BIGNUM **verifier, const BIGNUM *N,
761                            const BIGNUM *g)
762 {
763     return SRP_create_verifier_BN_ex(user, pass, salt, verifier, N, g, NULL,
764                                      NULL);
765 }
766 #endif