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