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