Coverage Report

Created: 2018-08-29 13:53

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