Coverage Report

Created: 2025-12-31 06:58

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