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 return NULL;
196 ret->N = NULL;
197 ret->g = NULL;
198 ret->s = NULL;
199 ret->v = NULL;
200 ret->id = NULL;
201 ret->info = NULL;
202 return ret;
203 }
204
SRP_user_pwd_set_gN(SRP_user_pwd * vinfo,const BIGNUM * g,const BIGNUM * N)205 void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
206 const BIGNUM *N)
207 {
208 vinfo->N = N;
209 vinfo->g = g;
210 }
211
SRP_user_pwd_set1_ids(SRP_user_pwd * vinfo,const char * id,const char * info)212 int SRP_user_pwd_set1_ids(SRP_user_pwd *vinfo, const char *id,
213 const char *info)
214 {
215 OPENSSL_free(vinfo->id);
216 OPENSSL_free(vinfo->info);
217 if (id != NULL && NULL == (vinfo->id = OPENSSL_strdup(id)))
218 return 0;
219 return (info == NULL || NULL != (vinfo->info = OPENSSL_strdup(info)));
220 }
221
SRP_user_pwd_set_sv(SRP_user_pwd * vinfo,const char * s,const char * v)222 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
223 const char *v)
224 {
225 unsigned char tmp[MAX_LEN];
226 int len;
227
228 vinfo->v = NULL;
229 vinfo->s = NULL;
230
231 len = t_fromb64(tmp, sizeof(tmp), v);
232 if (len < 0)
233 return 0;
234 if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
235 return 0;
236 len = t_fromb64(tmp, sizeof(tmp), s);
237 if (len < 0)
238 goto err;
239 vinfo->s = BN_bin2bn(tmp, len, NULL);
240 if (vinfo->s == NULL)
241 goto err;
242 return 1;
243 err:
244 BN_free(vinfo->v);
245 vinfo->v = NULL;
246 return 0;
247 }
248
SRP_user_pwd_set0_sv(SRP_user_pwd * vinfo,BIGNUM * s,BIGNUM * v)249 int SRP_user_pwd_set0_sv(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
250 {
251 BN_free(vinfo->s);
252 BN_clear_free(vinfo->v);
253 vinfo->v = v;
254 vinfo->s = s;
255 return (vinfo->s != NULL && vinfo->v != NULL);
256 }
257
srp_user_pwd_dup(SRP_user_pwd * src)258 static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
259 {
260 SRP_user_pwd *ret;
261
262 if (src == NULL)
263 return NULL;
264 if ((ret = SRP_user_pwd_new()) == NULL)
265 return NULL;
266
267 SRP_user_pwd_set_gN(ret, src->g, src->N);
268 if (!SRP_user_pwd_set1_ids(ret, src->id, src->info)
269 || !SRP_user_pwd_set0_sv(ret, BN_dup(src->s), BN_dup(src->v))) {
270 SRP_user_pwd_free(ret);
271 return NULL;
272 }
273 return ret;
274 }
275
SRP_VBASE_new(char * seed_key)276 SRP_VBASE *SRP_VBASE_new(char *seed_key)
277 {
278 SRP_VBASE *vb = OPENSSL_malloc(sizeof(*vb));
279
280 if (vb == NULL)
281 return NULL;
282 if ((vb->users_pwd = sk_SRP_user_pwd_new_null()) == NULL
283 || (vb->gN_cache = sk_SRP_gN_cache_new_null()) == NULL) {
284 sk_SRP_user_pwd_free(vb->users_pwd);
285 OPENSSL_free(vb);
286 return NULL;
287 }
288 vb->default_g = NULL;
289 vb->default_N = NULL;
290 vb->seed_key = NULL;
291 if ((seed_key != NULL) && (vb->seed_key = OPENSSL_strdup(seed_key)) == NULL) {
292 sk_SRP_user_pwd_free(vb->users_pwd);
293 sk_SRP_gN_cache_free(vb->gN_cache);
294 OPENSSL_free(vb);
295 return NULL;
296 }
297 return vb;
298 }
299
SRP_VBASE_free(SRP_VBASE * vb)300 void SRP_VBASE_free(SRP_VBASE *vb)
301 {
302 if (!vb)
303 return;
304 sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
305 sk_SRP_gN_cache_free(vb->gN_cache);
306 OPENSSL_free(vb->seed_key);
307 OPENSSL_free(vb);
308 }
309
SRP_gN_new_init(const char * ch)310 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
311 {
312 unsigned char tmp[MAX_LEN];
313 int len;
314 SRP_gN_cache *newgN = OPENSSL_malloc(sizeof(*newgN));
315
316 if (newgN == NULL)
317 return NULL;
318
319 len = t_fromb64(tmp, sizeof(tmp), ch);
320 if (len < 0)
321 goto err;
322
323 if ((newgN->b64_bn = OPENSSL_strdup(ch)) == NULL)
324 goto err;
325
326 if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
327 return newgN;
328
329 OPENSSL_free(newgN->b64_bn);
330 err:
331 OPENSSL_free(newgN);
332 return NULL;
333 }
334
SRP_gN_free(SRP_gN_cache * gN_cache)335 static void SRP_gN_free(SRP_gN_cache *gN_cache)
336 {
337 if (gN_cache == NULL)
338 return;
339 OPENSSL_free(gN_cache->b64_bn);
340 BN_free(gN_cache->bn);
341 OPENSSL_free(gN_cache);
342 }
343
SRP_get_gN_by_id(const char * id,STACK_OF (SRP_gN)* gN_tab)344 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
345 {
346 int i;
347
348 SRP_gN *gN;
349 if (gN_tab != NULL) {
350 for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
351 gN = sk_SRP_gN_value(gN_tab, i);
352 if (gN && (id == NULL || strcmp(gN->id, id) == 0))
353 return gN;
354 }
355 }
356
357 return SRP_get_default_gN(id);
358 }
359
SRP_gN_place_bn(STACK_OF (SRP_gN_cache)* gN_cache,char * ch)360 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
361 {
362 int i;
363 if (gN_cache == NULL)
364 return NULL;
365
366 /* search if we have already one... */
367 for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
368 SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
369 if (strcmp(cache->b64_bn, ch) == 0)
370 return cache->bn;
371 }
372 { /* it is the first time that we find it */
373 SRP_gN_cache *newgN = SRP_gN_new_init(ch);
374 if (newgN) {
375 if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
376 return newgN->bn;
377 SRP_gN_free(newgN);
378 }
379 }
380 return NULL;
381 }
382
383 /*
384 * This function parses the verifier file generated by the srp app.
385 * The format for each entry is:
386 * V base64(verifier) base64(salt) username gNid userinfo(optional)
387 * or
388 * I base64(N) base64(g)
389 * Note that base64 is the SRP variant of base64 encoding described
390 * in t_fromb64().
391 */
392
SRP_VBASE_init(SRP_VBASE * vb,char * verifier_file)393 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
394 {
395 int error_code = SRP_ERR_MEMORY;
396 STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
397 char *last_index = NULL;
398 int i;
399 char **pp;
400
401 SRP_gN *gN = NULL;
402 SRP_user_pwd *user_pwd = NULL;
403
404 TXT_DB *tmpdb = NULL;
405 BIO *in = BIO_new(BIO_s_file());
406
407 if (SRP_gN_tab == NULL)
408 goto err;
409
410 error_code = SRP_ERR_OPEN_FILE;
411
412 if (verifier_file == NULL) {
413 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
414 goto err;
415 }
416
417 if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
418 goto err;
419
420 error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
421
422 if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
423 goto err;
424
425 error_code = SRP_ERR_MEMORY;
426
427 if (vb->seed_key) {
428 last_index = SRP_get_default_gN(NULL)->id;
429 }
430 for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
431 pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
432 if (pp[DB_srptype][0] == DB_SRP_INDEX) {
433 /*
434 * we add this couple in the internal Stack
435 */
436
437 if ((gN = OPENSSL_malloc(sizeof(*gN))) == NULL)
438 goto err;
439
440 if ((gN->id = OPENSSL_strdup(pp[DB_srpid])) == NULL
441 || (gN->N = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
442 == NULL
443 || (gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
444 == NULL
445 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
446 goto err;
447
448 gN = NULL;
449
450 if (vb->seed_key != NULL) {
451 last_index = pp[DB_srpid];
452 }
453 } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
454 /* it is a user .... */
455 const SRP_gN *lgN;
456
457 if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
458 error_code = SRP_ERR_MEMORY;
459 if ((user_pwd = SRP_user_pwd_new()) == NULL)
460 goto err;
461
462 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
463 if (!SRP_user_pwd_set1_ids
464 (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
465 goto err;
466
467 error_code = SRP_ERR_VBASE_BN_LIB;
468 if (!SRP_user_pwd_set_sv
469 (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
470 goto err;
471
472 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
473 goto err;
474 user_pwd = NULL; /* abandon responsibility */
475 }
476 }
477 }
478
479 if (last_index != NULL) {
480 /* this means that we want to simulate a default user */
481
482 if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
483 error_code = SRP_ERR_VBASE_BN_LIB;
484 goto err;
485 }
486 vb->default_g = gN->g;
487 vb->default_N = gN->N;
488 gN = NULL;
489 }
490 error_code = SRP_NO_ERROR;
491
492 err:
493 /*
494 * there may be still some leaks to fix, if this fails, the application
495 * terminates most likely
496 */
497
498 if (gN != NULL) {
499 OPENSSL_free(gN->id);
500 OPENSSL_free(gN);
501 }
502
503 SRP_user_pwd_free(user_pwd);
504
505 TXT_DB_free(tmpdb);
506 BIO_free_all(in);
507
508 sk_SRP_gN_free(SRP_gN_tab);
509
510 return error_code;
511
512 }
513
find_user(SRP_VBASE * vb,char * username)514 static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
515 {
516 int i;
517 SRP_user_pwd *user;
518
519 if (vb == NULL)
520 return NULL;
521
522 for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
523 user = sk_SRP_user_pwd_value(vb->users_pwd, i);
524 if (strcmp(user->id, username) == 0)
525 return user;
526 }
527
528 return NULL;
529 }
530
SRP_VBASE_add0_user(SRP_VBASE * vb,SRP_user_pwd * user_pwd)531 int SRP_VBASE_add0_user(SRP_VBASE *vb, SRP_user_pwd *user_pwd)
532 {
533 if (sk_SRP_user_pwd_push(vb->users_pwd, user_pwd) <= 0)
534 return 0;
535 return 1;
536 }
537
538 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
539 /*
540 * DEPRECATED: use SRP_VBASE_get1_by_user instead.
541 * This method ignores the configured seed and fails for an unknown user.
542 * Ownership of the returned pointer is not released to the caller.
543 * In other words, caller must not free the result.
544 */
SRP_VBASE_get_by_user(SRP_VBASE * vb,char * username)545 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
546 {
547 return find_user(vb, username);
548 }
549 # endif
550
551 /*
552 * Ownership of the returned pointer is released to the caller.
553 * In other words, caller must free the result once done.
554 */
SRP_VBASE_get1_by_user(SRP_VBASE * vb,char * username)555 SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
556 {
557 SRP_user_pwd *user;
558 unsigned char digv[SHA_DIGEST_LENGTH];
559 unsigned char digs[SHA_DIGEST_LENGTH];
560 EVP_MD_CTX *ctxt = NULL;
561 EVP_MD *md = NULL;
562
563 if (vb == NULL)
564 return NULL;
565
566 if ((user = find_user(vb, username)) != NULL)
567 return srp_user_pwd_dup(user);
568
569 if ((vb->seed_key == NULL) ||
570 (vb->default_g == NULL) || (vb->default_N == NULL))
571 return NULL;
572
573 /* if the user is unknown we set parameters as well if we have a seed_key */
574
575 if ((user = SRP_user_pwd_new()) == NULL)
576 return NULL;
577
578 SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
579
580 if (!SRP_user_pwd_set1_ids(user, username, NULL))
581 goto err;
582
583 if (RAND_priv_bytes(digv, SHA_DIGEST_LENGTH) <= 0)
584 goto err;
585 md = EVP_MD_fetch(NULL, SN_sha1, NULL);
586 if (md == NULL)
587 goto err;
588 ctxt = EVP_MD_CTX_new();
589 if (ctxt == NULL
590 || !EVP_DigestInit_ex(ctxt, md, NULL)
591 || !EVP_DigestUpdate(ctxt, vb->seed_key, strlen(vb->seed_key))
592 || !EVP_DigestUpdate(ctxt, username, strlen(username))
593 || !EVP_DigestFinal_ex(ctxt, digs, NULL))
594 goto err;
595 EVP_MD_CTX_free(ctxt);
596 ctxt = NULL;
597 EVP_MD_free(md);
598 md = NULL;
599 if (SRP_user_pwd_set0_sv(user,
600 BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
601 BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
602 return user;
603
604 err:
605 EVP_MD_free(md);
606 EVP_MD_CTX_free(ctxt);
607 SRP_user_pwd_free(user);
608 return NULL;
609 }
610
611 /*
612 * create a verifier (*salt,*verifier,g and N are in base64)
613 */
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)614 char *SRP_create_verifier_ex(const char *user, const char *pass, char **salt,
615 char **verifier, const char *N, const char *g,
616 OSSL_LIB_CTX *libctx, const char *propq)
617 {
618 int len;
619 char *result = NULL, *vf = NULL;
620 const BIGNUM *N_bn = NULL, *g_bn = NULL;
621 BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;
622 unsigned char tmp[MAX_LEN];
623 unsigned char tmp2[MAX_LEN];
624 char *defgNid = NULL;
625 int vfsize = 0;
626
627 if ((user == NULL) ||
628 (pass == NULL) || (salt == NULL) || (verifier == NULL))
629 goto err;
630
631 if (N) {
632 if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)
633 goto err;
634 N_bn_alloc = BN_bin2bn(tmp, len, NULL);
635 if (N_bn_alloc == NULL)
636 goto err;
637 N_bn = N_bn_alloc;
638 if ((len = t_fromb64(tmp, sizeof(tmp), g)) <= 0)
639 goto err;
640 g_bn_alloc = BN_bin2bn(tmp, len, NULL);
641 if (g_bn_alloc == NULL)
642 goto err;
643 g_bn = g_bn_alloc;
644 defgNid = "*";
645 } else {
646 SRP_gN *gN = SRP_get_default_gN(g);
647 if (gN == NULL)
648 goto err;
649 N_bn = gN->N;
650 g_bn = gN->g;
651 defgNid = gN->id;
652 }
653
654 if (*salt == NULL) {
655 if (RAND_bytes_ex(libctx, tmp2, SRP_RANDOM_SALT_LEN, 0) <= 0)
656 goto err;
657
658 s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
659 } else {
660 if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)
661 goto err;
662 s = BN_bin2bn(tmp2, len, NULL);
663 }
664 if (s == NULL)
665 goto err;
666
667 if (!SRP_create_verifier_BN_ex(user, pass, &s, &v, N_bn, g_bn, libctx,
668 propq))
669 goto err;
670
671 if (BN_bn2bin(v, tmp) < 0)
672 goto err;
673 vfsize = BN_num_bytes(v) * 2;
674 if (((vf = OPENSSL_malloc(vfsize)) == NULL))
675 goto err;
676 if (!t_tob64(vf, tmp, BN_num_bytes(v)))
677 goto err;
678
679 if (*salt == NULL) {
680 char *tmp_salt;
681
682 if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
683 goto err;
684 }
685 if (!t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN)) {
686 OPENSSL_free(tmp_salt);
687 goto err;
688 }
689 *salt = tmp_salt;
690 }
691
692 *verifier = vf;
693 vf = NULL;
694 result = defgNid;
695
696 err:
697 BN_free(N_bn_alloc);
698 BN_free(g_bn_alloc);
699 OPENSSL_clear_free(vf, vfsize);
700 BN_clear_free(s);
701 BN_clear_free(v);
702 return result;
703 }
704
SRP_create_verifier(const char * user,const char * pass,char ** salt,char ** verifier,const char * N,const char * g)705 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
706 char **verifier, const char *N, const char *g)
707 {
708 return SRP_create_verifier_ex(user, pass, salt, verifier, N, g, NULL, NULL);
709 }
710
711 /*
712 * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
713 * then the provided salt will be used. On successful exit *verifier will point
714 * to a newly allocated BIGNUM containing the verifier and (if a salt was not
715 * provided) *salt will be populated with a newly allocated BIGNUM containing a
716 * random salt.
717 * The caller is responsible for freeing the allocated *salt and *verifier
718 * BIGNUMS.
719 */
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)720 int SRP_create_verifier_BN_ex(const char *user, const char *pass, BIGNUM **salt,
721 BIGNUM **verifier, const BIGNUM *N,
722 const BIGNUM *g, OSSL_LIB_CTX *libctx,
723 const char *propq)
724 {
725 int result = 0;
726 BIGNUM *x = NULL;
727 BN_CTX *bn_ctx = BN_CTX_new_ex(libctx);
728 unsigned char tmp2[MAX_LEN];
729 BIGNUM *salttmp = NULL, *verif;
730
731 if ((user == NULL) ||
732 (pass == NULL) ||
733 (salt == NULL) ||
734 (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
735 goto err;
736
737 if (*salt == NULL) {
738 if (RAND_bytes_ex(libctx, tmp2, SRP_RANDOM_SALT_LEN, 0) <= 0)
739 goto err;
740
741 salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
742 if (salttmp == NULL)
743 goto err;
744 } else {
745 salttmp = *salt;
746 }
747
748 x = SRP_Calc_x_ex(salttmp, user, pass, libctx, propq);
749 if (x == NULL)
750 goto err;
751
752 verif = BN_new();
753 if (verif == NULL)
754 goto err;
755
756 if (!BN_mod_exp(verif, g, x, N, bn_ctx)) {
757 BN_clear_free(verif);
758 goto err;
759 }
760
761 result = 1;
762 *salt = salttmp;
763 *verifier = verif;
764
765 err:
766 if (salt != NULL && *salt != salttmp)
767 BN_clear_free(salttmp);
768 BN_clear_free(x);
769 BN_CTX_free(bn_ctx);
770 return result;
771 }
772
SRP_create_verifier_BN(const char * user,const char * pass,BIGNUM ** salt,BIGNUM ** verifier,const BIGNUM * N,const BIGNUM * g)773 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
774 BIGNUM **verifier, const BIGNUM *N,
775 const BIGNUM *g)
776 {
777 return SRP_create_verifier_BN_ex(user, pass, salt, verifier, N, g, NULL,
778 NULL);
779 }
780 #endif
781